@RequestMapping(value = "/user/{username:.+}",method = RequestMethod.GET,produces = "application/json")
@ResponseBody
User user(@PathVariable String username) {
User user = userRepository.findByUsername(username);
if (user == null)
throw new UserNotFoundException("User not found");
return user;
}
这是表示该动作的方法. Controller使用@RestController进行注释
解决了
应覆盖内容类型协商机制.
Explonation:http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc
码:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.mediaType("pl",MediaType.APPLICATION_JSON);
}
PathMatchConfigurer尝试将每个/controller/path.*与每个后缀匹配,尝试使用ContentNegotiationManager查找内容类型.您可以通过禁用此功能或仅在.*是已注册的后缀时尝试更改此行为.见:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-path-matching
您应该调用pathMatchConfigurer.setUseRegisteredSuffixPatternMatch(true)或pathMatchConfigurer. setUseSuffixPatternMatch(假)
老答案:)
我认为Spring MVC错误地假设.pl是一个扩展,并为这种媒体类型查找HTTPMessageConverter.在这种情况下创建一个转换器是没有意义的,但是将它标记为不同的媒体类型会起作用吗?我认为这只是一种解决方法.
我还认为您的@RequestMapping值可能很简单:value =“/ user / {username}” – 您正在使用RegEx.对于您的用户名变量,这实际上意味着您仍然匹配整个模式.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。