Java Spring POST在HTTP 415和400上失败
我正在尝试使用fiddler对spring rest API进行POST调用Java Spring POST在HTTP 415和400上失败,java,spring,post,http-post,http-status-code-415,Java,Spring,Post,Http Post,Http Status Code 415,我正在尝试使用fiddler对spring rest API进行POST调用 @RequestMapping(value = "/GetPlanByBasicContext/", method = RequestMethod.POST) public @ResponseBody TravelPlan getPlanByBasicContext(@RequestBody BasicPlanContext b) { return planService.getPlan(b)); } 关于小
@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
邮政有效载荷:
{
"sourceLocation": "",
"destinationLocation": "",
"modeOfTransport": "car",
"travellers": {
"age1to16": 0,
"age17to30": 0,
"age31to50": 0,
"age50plus": 0
},
"dates": {
"startDate": "",
"endDate": ""
}
}
有效负载中的属性与BasicPanContext类中的属性相同,还有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.
尝试将@RequestBody替换为@ModelAttribute,但没有帮助
此外,我还有以下LIB:
<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>
遵循POST方法
@RequestMapping(..., headers="Accept=application/json")
这导致
400客户端发送的请求语法不正确。
您试图发送JSON负载,但服务器拒绝状态为415的请求,这表明请求的媒体类型导致问题
首先,您需要定义请求方法接受JSON有效负载
@RequestMapping(..., headers="Accept=application/json")
而fiddler请求应该包括标题
Content-Type: application/json
我在前面尝试过这个方法,结果抛出400个错误请求,“客户端发送的请求在语法上不正确”。@varsh使用400的原因是JSON无法解析为
basicPanContext
对象。你在服务器日志中有错误跟踪吗?我在服务器控制台上没有看到任何日志,除了SLF4J:未能加载类“org.SLF4J.impl.StaticLoggerBinder”。@varsh非常感谢,我得到了日志,上面写着“JsonMappingException:没有为类型[simple type,class]找到合适的构造函数”:无法从“JSON对象”实例化。以下链接有助于解决内部类的问题:
Content-Type: application/json