415使用consume的状态代码= MediaType.APPLICATION_JSON_VALUE

时间:2016-06-01 17:34:04

标签: java spring rest spring-mvc

我正在尝试使用spring mvc rest将json传入java对象。但获得415状态代码。

弹簧servlet.xml中

<context:component-scan base-package="com.xyz.controllers" />
<mvc:annotation-driven/>

RestController:

@RequestMapping(value = "/hellouser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> helloHomer(@RequestBody Home home) {
    return new ResponseEntity<String>(home.getFirst() + home.getSecond(),
            HttpStatus.OK);
}

家庭豆:

public class Home {
    private String first = "defaultFirst";
    private String second = "defaultSecond";
    public String getFirst() {
        return first;
    }
    public void setFirst(String first) {
        this.first = first;
    }
    public String getSecond() {
        return second;
    }
    public void setSecond(String second) {
        this.second = second;
    }
}

的pom.xml:

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

使用不同的休息客户端测试但获得415状态代码。

Accept=application/json
{"first" : "first"
  "second" : "second"}

尝试了解问题并修复。

由于

1 个答案:

答案 0 :(得分:1)

在从rest客户端调用服务时将Content-Type = application / json作为请求标头传递