【问题标题】:Spring roo rest service 415 unsupported Media Type ExceptionSpring roo rest service 415 unsupported Media Type Exception
【发布时间】: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


【解决方案1】:

在你的标题(邮递员)中添加

Content-Type: application/json

【讨论】:

  • 您好,除了来自 Accept:application/json 您是否还在 postman 中定义了 Content-Type?
  • 能否在您的 CustomUserInfo 类中添加一个默认构造函数(无参数构造函数)?
  • 我添加了以下代码仍然得到相同的错误 public CustomUserInfo(){ System.out.println("Default Constructor"); }
【解决方案2】:

感谢大家的支持,我在 pom.xml 中添加了 Jackson 库的一个解决方案,&lt;mvc:annotation-driven/&gt; 标签解决了我的问题。以下是我添加的库

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2011-05-13
    相关资源
    最近更新 更多