0

我正在尝试使用提琴手对 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/

标题:

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 中的属性与类 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:尝试在标题中添加以下内容,

Content-Type: application/json

并遵循 POST 方法,

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

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

4

1 回答 1

0

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

首先,您需要定义请求方法接受 JSON 有效负载

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

提琴手请求应包含标头

Content-Type: application/json
于 2016-04-08T13:04:33.590 回答