使用Spring Boot,Controller请求返回的参数类型是ResponseBody , 如果请求的时候使用使用配置的默认请求扩展名,例如.html,Spring MVC会抛出一个type=Not Acceptable, status=406错误,如下:
1Whitelabel Error Page 2 3This application has no explicit mapping for /error, so you are seeing this as a fallback. 4 5Mon Jan 23 17:14:35 CST 2017 6There was an unexpected error (type=Not Acceptable, status=406). 7Could not find acceptable representation
请求处理的Controller代码如下:
1/** 2 * 延时获取用户端浏览器的跟踪ID 3 * @param request 4 * @param response 5 * @param orgi 6 * @param appid 7 * @param userid 8 * @param sign 9 * @return 10 */ 11 @RequestMapping("/online") 12 @Menu(type = "im" , subtype = "online" , access = true) 13 public @ResponseBody OnlineUser online(HttpServletRequest request , HttpServletResponse response , @Valid String orgi , @Valid String appid, @Valid String userid , @Valid String sign){ 14 OnlineUser onlineUser = null ; 15 if(!StringUtils.isBlank(sign)){ 16 onlineUser = OnlineUserUtils.online(super.getIMUser(request , sign) , orgi ,sign , UKDataContext.OnlineUserTypeStatus.WEBIM.toString(), request); 17 } 18 return onlineUser; 19 }
处理办法:
1、使用JSON请求Controller,增加 org.codehaus.jackson 的依赖
1<dependency> 2 <groupId>com.fasterxml.jackson.dataformat</groupId> 3 <artifactId>jackson-dataformat-xml</artifactId> 4</dependency>
请求的URL使用http://xxx.xxx.xxx.xxx:8080/online.json,访问正常。
2、覆盖Application.java里的 configureContentNegotiation 方法,代码如下:
1@SpringBootApplication 2public class Application extends WebMvcConfigurerAdapter { 3 4 5 @Override 6 public void configureContentNegotiation(ContentNegotiationConfigurer config) { 7 config.favorPathExtension(false); 8 } 9 public static void main(String[] args) { 10 SpringApplication.run(Application.class, args); 11 } 12}
方法2中关键的代码在于 config.favorPathExtension(false);
favorPathExtension表示支持后缀匹配,
ignoreAcceptHeader默认为fasle,表示accept-header匹配,defaultContentType开启默认匹配。