简体   繁体   中英

Spring roo rest service 415 unsupported Media Type Exception

I am getting 415 Unsupported Media type error when I add @RequestBody annotation in my function please find below snippet

 @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);
    }  

Please find below is my CustomUserInfo class

@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

And Following is the api I am trying to hit through POSTMAN

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

在标题(邮递员)中添加

Content-Type: application/json

Thanks Guys for your support, I got one solution added Jackson library in my pom.xml and <mvc:annotation-driven/> tag solved my issue. following is the library I have added

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1</version>
        </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM