Spring roo rest service 415不支持媒体类型例外

时间:2016-09-19 12:01:05

标签: spring spring-mvc spring-roo

当我在我的函数中添加@RequestBody注释时,我得到415不支持的媒体类型错误,请在下面的代码段中找到

 @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"
    }

2 个答案:

答案 0 :(得分:0)

你的标题中的

(邮递员)添加

Content-Type: application/json

答案 1 :(得分:0)

感谢大家的支持,我在我的pom.xml中添加了一个解决方案杰克逊库,struct S { virtual ~S() = 0; }; S::~S() {} 标签解决了我的问题。以下是我添加的库

<mvc:annotation-driven/>