Spring4新特性——Web开发的增强
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
Spring4新特性——注解、脚本、任务、MVC等其他特性改进
从Spring4开始,Spring以Servlet3为进行开发,如果用Spring MVC 测试框架的话需要指定Servlet3兼容的jar包(因为其Mock的对象都是基于Servlet3的)。另外为了方便Rest开发,通过新的@RestController指定在控制器上,这样就不需要在每个@RequestMapping方法上加 @ResponseBody了。而且添加了一个AsyncRestTemplate ,支持REST客户端的异步无阻塞支持。
1、@RestController
@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}";
}
}
其实现就是在@@RestController中加入@ResponseBody:
@org.springframework.stereotype.Controller
@org.springframework.web.bind.annotation.ResponseBody
public @interface RestController {
}
这样当你开发Rest服务器端的时候,spring-mvc配置文件需要的代码极少,可能就仅需如下一行:
<context:component-scan base-package="com.sishuok.spring4"/>
<mvc:annotation-driven/>
2、mvc:annotation-driven配置变化
统一风格;将 enableMatrixVariables改为enable-matrix-variables属性;将ignoreDefaultModelOnRedirect改为ignore-default-model-on-redirect。
3、提供AsyncRestTemplate用于客户端非阻塞异步支持。
3.1、服务器端
对于服务器端的springmvc开发可以参考https://github.com/zhangkaitao/servlet3-showcase中的chapter3-springmvc
@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 * 1000); //暂停两秒
User user = new User();
user.setId(1L);
user.setName("haha");
return user;
}
};
}
}
非常简单,服务器端暂停10秒再返回结果(但是服务器也是非阻塞的)。具体参考我github上的代码。
3.2、客户端
public static void main(String[] args) {
AsyncRestTemplate template = new AsyncRestTemplate();
//调用完后立即返回(没有阻塞)
ListenableFuture<ResponseEntity<User>> future = template.getForEntity("http://localhost:9080/spring4/api", User.class);
//设置异步回调
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");
}
此处使用Future来完成非阻塞,这样的话我们也需要给它一个回调接口来拿结果; Future和Callable是一对,一个消费结果,一个产生结果。调用完模板后会立即返回,不会阻塞;有结果时会调用其回调。
AsyncRestTemplate默认使用SimpleClientHttpRequestFactory,即通过java.net.HttpURLConnection实现;另外我们也可以使用apache的http components;使用template.setAsyncRequestFactory(new HttpComponentsAsyncClientHttpRequestFactory());设置即可。
另外在开发时尽量不要自己注册如:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
尽量使用
<mvc:annotation-driven/>
它设计的已经足够好,使用子元素可以配置我们需要的配置。
且不要使用老版本的:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
否则可能得到如下异常:
写道
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.)
http://www.cnblogs.com/yangzhilong/p/3725128.html 这里有更详细的使用方法。
博主 有没有 spring4+hibernate4的demo 我想学习下
spring 4的配置demo有吗?求一份!!
我也遇到过这问题,应该是因为 使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。
张哥,在android客户端请求的时候返回json出现406错误!!
试试返回ModelAndView?
涛 哥,使用了 @RestController 注解后就只能返回json数据吗,我想返回一个视图,比如说index.jsp 还可以吗,试了下 只返回一个字符串 index
晚上回家发你
估计是你哪个地配置的不对,MappingJacksonHttpMessageConverter升级到MappingJackson2HttpMessageConverter吧,(需要jackson2版本)
Circular view path : would dispatch back to the current handler URL again
从错误看访问login好像死循环了;估计不是这段代码的问题;可能是其他配置的问题;如果方便把相关配置文件站内信我
直接写
没有问题,但是换成这样下面这种写法之后就会报错了
提示这个错
后续有时间的话想试试,然后分析;这个要看业余时间是否充分了
楼主不知道有没有关于websocket的配置分析呢?
这些都是我测试通过之后才写的
这个倒不影响 可以写个filter判断下是客户端的 还是浏览器过来的 针对ie单独设置响应头
因为IE下用 application/json 返回json,会发生弹出下载框的情况,所以一般我都定义成text/html返回json。 这样和AsyncRestTemplate 有点冲突了
@ResponseBody 放在函数方法上可以,放在类上无效。
您那有这个的测试工程吗
这是@RestController注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Controller @ResponseBody public @interface RestController {
}
嗯,它在请求时会带着Accept:application/json, application/*+json 过去;springmvc会自动响应Content-Type:application/json;
估计是不是你没加 jackson2的jar包?
不用配其他的啊;@RestController = @Controller + @ResponseBody
另外 AsyncRestTemplate 使用必须使用 application/json 返回json
如果使用text/html会报,无法转换成User对象。