有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用JSONDOC将标题指定为application/json

我正在使用Spring4设计RESTAPI。但当我使用JsonDoc记录它时,它给了我一个错误:

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

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");
}

我如何测试它


共 (2) 个答案

  1. # 1 楼答案

    我相信这与JSONDoc无关。您需要检查发送的内容类型是否有效,比如application/json

    还请检查TestDto JSON表示是否正确转换为JAVA对象。如果JSON字符串无法转换java对象,Spring会抛出此错误

    If you are using Jackson API, You can use below sample to get exception and see cause.

        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();
        }
    
  2. # 2 楼答案

    检查内容类型和有效负载格式

    可能有一些属性不匹配