Spring POST HTTP 415 和 400 失败

Spring POST Fails HTTP 415 and 400

我正在尝试使用 fiddler 对 spring rest API、

进行 POST 调用
@RequestMapping(value = "/GetPlanByBasicContext/", method = RequestMethod.POST)
public @ResponseBody TravelPlan getPlanByBasicContext(@RequestBody BasicPlanContext b) {
    return planService.getPlan(b));
}

提琴手请求:

http://localhost:8080/now/travelPlan/GetPlanByBasicContext/

Header:

User-Agent: Fiddler
Host: localhost:8080
Content-Length: 248

POST 负载:

{
    "sourceLocation": "",
    "destinationLocation": "",
    "modeOfTransport": "car", 
    "travellers": {
        "age1to16": 0,
        "age17to30": 0,
        "age31to50": 0,
        "age50plus": 0
    },
    "dates": {
        "startDate": "",
        "endDate": ""
    }
}

payload中的属性与class BasicPlanContext中的属性相同,还有getter和setter。

我收到以下错误:

 415 Unsupported Media Type
 The server refused this request because the request entity is in a format not supported 
 by the requested resource for the requested method.

尝试用@ModelAttribute 替换@RequestBody,但没有用。

我还有以下库:

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

编辑 1: 尝试将以下内容添加到 header、

Content-Type: application/json

然后按照 POST 方法,

@RequestMapping(..., headers="Accept=application/json")

这导致 400 The request sent by the client was syntactically incorrect.

您正在尝试发送 JSON 负载,但服务器拒绝了状态为 415 的请求,这表明请求的媒体类型导致了问题。

首先你需要定义请求方法接受JSON payloads

@RequestMapping(..., headers="Accept=application/json")

提琴手请求应包括 header

Content-Type: application/json