我使用Spring4来设计我的REST API。但是当我使用JsonDoc记录它时,它给出了一个错误:
服务器拒绝此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
Apache Tomcat/7.0.62
", "status": 415, "statusText": "Unsupported Media Type" }
@ApiMethod
@ApiHeaders(headers={ @ApiHeader(name="Content-Type", allowedvalues="application/json",description="application/json")})
@RequestMapping(value="/test" ,method=RequestMethod.POST)
public @ApiResponseObject @ResponseBody ResponseMessage test(@ApiBodyObject @RequestBody TestDto test){
System.out.println(test.getId());
return testService.addTestMessage("hello demo");
}
我该怎么测试呢?
检查您的内容类型并检查您的有效负载格式。
可能存在一些不匹配的属性。
我相信它与JSONDoc无关。您需要检查是否发送了有效的Content-Type,如application / json。
还请检查TestDto JSON表示是否正确转换为JAVA对象。如果您的JSON字符串无法转换java对象,则Spring会抛出此错误。
如果您使用的是Jackson API,则可以使用以下示例获取异常并查看原因。
String myJsonString="Your JSON String";
ObjectMapper mapper = new ObjectMapper();
try {
mapper.readValue(myJsonString,TestDto.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}