Spring4 new features--web development enhancements
Spring4 new features-generic constrained dependency Injection
Spring4 new features-other improvements to the core container
Spring4 new features--web development enhancements
Starting with Spring4, Spring is developed with Servlet3, and if you use the Spring MVC test framework, you need to specify SERVLET3 compatible jar packages (because their mock objects are Servlet3 based). In addition, to facilitate rest development, the new @restcontroller is specified on the controller so that there is no need to add on each @requestmapping method @ResponseBody了。而且添加了一个 AsyncRestTemplate to support asynchronous nonblocking support for rest clients.
1. @RestController
View Copy to Clipboard printing
@RestController
Public class Usercontroller {
Private UserService UserService;
@Autowired
Public Usercontroller (UserService userservice) {
this. UserService = UserService;
}
@RequestMapping ("/test")
Public User view () {
User user = new user ();
User.setid (1L);
User.setname ("haha");
return user;
}
@RequestMapping ("/test2")
Public String View2 () {
return "{\" id\ ": 1}";
}
}
The implementation is to include @responsebody in the @ @RestController:
View Copy to Clipboard printing
@org. Springframework.stereotype.Controller
@org. springframework.web.bind.annotation.ResponseBody
Public @interface Restcontroller {
}
So when you develop the rest server side, the SPRING-MVC configuration file requires very little code, possibly just the following line:
View Copy to Clipboard printing
<context:component-scan base-package ="Com.sishuok.spring4"/>
<mvc:annotation-driven/>
2. Mvc:annotation-driven Configuration Changes
Unified style; Change Enablematrixvariables to Enable-matrix-variables property ; Change Ignoredefaultmodelonredirect to Ignore-default-model-on-redirect.
3, provide asyncresttemplate for client non-blocking asynchronous support.
3.1. Server-side
For server-side SPRINGMVC development, refer to the CHAPTER3-SPRINGMVC in Https://github.com/zhangkaitao/servlet3-showcase
View Copy to Clipboard printing
@RestController
Public class Usercontroller {
Private UserService UserService;
@Autowired
Public Usercontroller (UserService userservice) {
this. UserService = UserService;
}
@RequestMapping ("/api")
Public Callable<user> API () {
System.out.println ("=====hello");
return New Callable<user> () {
@Override
Public User call () throws Exception {
Thread.Sleep (10L * ); //Pause for two seconds
User user = new user ();
User.setid (1L);
User.setname ("haha");
return user;
}
};
}
}
Very simple, the server side pauses for 10 seconds and returns the result (but the server is also non-blocking). Refer to the code on my github for details.
3.2, the Client
View Copy to Clipboard printing
Public Static void Main (string[] args) {
Asyncresttemplate template = new asyncresttemplate ();
//return immediately after call (no blocking)
listenablefuture<responseentity<user>> future = template.getforentity ("http://localhost:9080/ Spring4/api ", User. class);
//Set asynchronous callbacks
Future.addcallback (new listenablefuturecallback<responseentity<user>> () {
@Override
Public void onsuccess (responseentity<user> result) {
System.out.println ("======client get Result:" + result.getbody ());
}
@Override
Public void onfailure (Throwable t) {
System.out.println ("======client failure:" + t);
}
});
System.out.println ("==no Wait");
}
The future is used here to do non-blocking, so we also need to give it a callback interface to get the results; the future and the callable are a pair, a consumption result, a result. When the template is finished, it returns immediately, does not block, and invokes its callback when there is a result.
Asyncresttemplate uses Simpleclienthttprequestfactory by default, which is achieved through java.net.HttpURLConnection, and we can also use Apache's HTTP Use Template.setasyncrequestfactory (new Httpcomponentsasyncclienthttprequestfactory ());
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1557325
Restcontroller in Java