Web development enhancement of new features of spring 4

Starting with spring 4, spring is developed with servlet3 as the, If you use spring MVC to test the framework, you need to specify a servlet 3 compatible jar package (because the mock objects are based on servlet3). In addition, in order to facilitate rest development, a new @ restcontroller is specified on the controller, so there is no need to add @ ResponseBody to each @ requestmapping method. In addition, an asyncresttemplate is added to support asynchronous non blocking support for rest clients.

1、@RestController

Java code

Its implementation is to add @ ResponseBody to @ @ restcontroller:

Java code

In this way, when you develop the rest server, the spring MVC configuration file requires very little code, and may only need the following line:

Java code

2. MVC: annotation driven configuration change

Unified style; Change enablematrixvariables to enable matrix variables attribute; Change ignoredefaultmodelonredirect to ignore default model on redirect.

3. Asyncresttemplate is provided for client-side non blocking asynchronous support.

3.1. Server side

Java code

Very simple. The server side pauses for 10 seconds and returns the result (but the server is also non blocking). For details, please refer to the code on GitHub.

3.2 client

Java code

Here, future is used to complete non blocking. In this case, we also need to give it a callback interface to get the results; Future and callable are a pair, one consumption result and the other production result. After calling the template, it will return immediately without blocking; The callback is called when there are results.

Asyncresttemplate uses simpleclienthttprequestfactory by default, that is, through Java net. Implementation of httpurlconnection; In addition, we can also use Apache's HTTP components; Use template setAsyncRequestFactory(new HttpComponentsAsyncClientHttpRequestFactory()); Just set.

In addition, try not to register yourself during development, such as:

Java code

Try to use

Java code

It is well designed enough to use child elements to configure the configuration we need.

And don't use the old version:

Java code

Otherwise, the following exceptions may be obtained:

Write

Circular view path [login]: would dispatch back to the current handler URL [/spring4/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view,due to default view name generation.)

summary

The above is the web development enhancement of the new features of spring4 introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>