@ResponseBody

Spring MVC + JSON = 406 Not Acceptable

僤鯓⒐⒋嵵緔 提交于 2019-12-07 21:27:57
在开发接口时发现了一个很好玩的问题,两个接口均是restful形式,参数在URL路径中传递,返回结果形式均为json,但是在接口测试时,一个接口正常,另一个接口报错:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.本文来分析一下其中的原因。   先介绍一下出错的接口情况,接口代码如下: /** * 验证email是否可用 * @param email * @return */ @ResponseBody @RequestMapping(value = "/emailCheck/{email}", method = RequestMethod.GET) @ApiOperation(value = "验证email是否可用", httpMethod = "GET", response = ApiResult.class, notes = "验证email是否可用") public ApiResult checkEmailValid(@ApiParam(required = true, name = "email",

@ResponseBody响应JSON 406

六眼飞鱼酱① 提交于 2019-12-07 12:28:36
搭建 SpringMVC(4.1) ,但是搭建完成以后发现使用 @ResponseBody的ajax无法访问,总是出现406的问题。 首先怀疑的是配置问题,经过查明,影响SpringMVC的 @ResponseBody注解的是: <mvc:annotation-driven />,我发现我的配置中存在这个注解。同时又使用Spring文档中的自动配置相关解析类的方式再进行测试,发现还是没有解决问题。 同时在网上找到相关问题,发现是 缺失jackson的jar 。 <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-lgpl</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.9.0</version> </dependency> 于是加上该依赖,可是发现问题还是没有解决。 有参考一下spring 文档 Spring 4 requires the recent Hibernate Validator 4.3+, and

【Spring reference】@ResponseBody注解

心已入冬 提交于 2019-11-27 11:09:50
Spring reference写到: Mapping the response body with the @ResponseBody annotation The @ResponseBodyannotation is similar to @RequestBody. This annotation can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name). For example: @RequestMapping(value = "/something", method = RequestMethod.PUT) @ResponseBody public String helloWorld() { return "Hello World"; } The above example will result in the text Hello World being written to the HTTP response stream. As with @RequestBody, Spring