【发布时间】:2016-09-19 12:01:04
【问题描述】:
当我在函数中添加@RequestBody 注释时出现 415 Unsupported Media type 错误,请在下面找到 sn-p
@RequestMapping(method=RequestMethod.POST, value="/registerUser", headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> registerUser(@RequestBody CustomUserInfo customUserInfo){
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
logger.info("in register user flow");
String responseMessage = null;
try{
if(customUserInfo != null){
//check if existing user
if(customUserService.isUserExist(customUserInfo)){
throw new UserAlreadyExistsException(customUserInfo.getEmail() + " Email Address already exist");
}else{
responseMessage = customUserService.saveCustomUserInfo(customUserInfo);
}
}
}catch(RoleNotFoundException e) {
logger.error("RoleNotFoundException", e);
}catch(Exception e){
logger.error("Exception occur while Register", e);
}
return new ResponseEntity<String>(responseMessage,headers,HttpStatus.OK);
}
请在下面找到我的 CustomUserInfo 类
@RooJson(deepSerialize = true)
@RooToString
public class CustomUserInfo implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String email;
private Set<String> roles;
//Setter and getters
以下是我试图通过 POSTMAN 访问的 api
URL: http://localhost:8080/<application-name>/auth/registerUser
Method: POST
Headers: Accept:application/json, Content-Type:application/json
Body:
{
"email": "abc@gmail.com",
"roles": ["ROLE_USER"],
"password": "abc123",
"username": "abc"
}
【问题讨论】:
-
您可以尝试使用 conumses="application/json",而不是在 @RequestMapping 中使用 headers 属性
-
嗨,在@RequestMapping 中,我已经用 headers = "consumes=application/json" 替换了标头,并将邮递员中的标头更改为消费:application/json 作为 Accept 和 Content-Type 给出 404 错误但仍然得到相同的 415 消费。
标签: spring spring-mvc spring-roo