@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;
}
Run Code Online (Sandbox Code Playgroud)
这是表示该动作的方法.控制器注释为@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);
}
Run Code Online (Sandbox Code Playgroud)