简体   繁体   English

Spring Roo Rest Service 415不支持的媒体类型异常

[英]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 我在函数中添加@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);
    }  

Please find below is my CustomUserInfo class 请在下面找到我的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

And Following is the api I am trying to hit through POSTMAN 以下是我正在尝试通过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"
    }

在标题(邮递员)中添加

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. 感谢Guys的支持,我在pom.xml中添加了一个Jackson图书馆解决方案,并且<mvc:annotation-driven/>标记解决了我的问题。 following is the library I have added 以下是我添加的库

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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