【问题标题】:Server returned HTTP response code: 415 Unsupported media type服务器返回 HTTP 响应代码:415 不支持的媒体类型
【发布时间】:2017-08-22 14:59:12
【问题描述】:

我有一个如下所示的休息网络服务。

@POST
@Path("/startProcess")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
public String startProcess(InputParams inputParams, @Context HttpServletRequest request, @Context HttpServletResponse response) {       
   ProjectBean projBean = new ProjectBean();
   Helper.loadProjectBean(inputParams, projBean);
   return "1";
}

现在我正在尝试使用下面的主程序来使用它。

public static void main(String[] args) throws Exception {
    StringBuffer response = new StringBuffer();
    String taigaServiceUrl = "http://localhost:8181/restServer/rest/TestWebService/startProcess/";
    URL url = new URL(taigaServiceUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/json");
    String userpass = "admin" + ":" + "admin";
    String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
    conn.setRequestProperty("Authorization", basicAuth);
    InputParams inputParams = new InputParams();
    inputParams.setXXX("xxxx");
    inputParams.setYYYY("123456");
    inputParams.setZZZZ("ZZZZ");
    String json = new Gson().toJson(inputParams);
    DataOutputStream os = new DataOutputStream (conn.getOutputStream());
    os.write(json.getBytes());
    os.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
    String inputLine;
    while ((inputLine = br.readLine()) != null) {
        response.append(inputLine);
    }
    br.close();
}

但每次我都遇到错误。

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 415 for URL: http://localhost:8181/restServer/rest/TestWebService/startProcess/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at scm.controllers.Test.main(Test.java:64)

根据错误,不支持媒体类型。在我的休息网络服务中,我正在使用 JSON,而在我的主程序中,我正在发送 JSON。那么它在哪里坏了呢?

【问题讨论】:

  • 不知道是否重要,但您可能需要添加请求标头/属性Accept: text/plain,并且您可能需要在网址末尾删除/。另外,不要使用DataOutputStream(反正你不是在做原始的Java数据类型)。

标签: java json rest jersey


【解决方案1】:

经过大量调试后,我找到了问题的解决方案。我需要在类路径中添加下面的罐子。实际上 Jersey 无法将 JSON 对象绑定到其余服务。

jackson-annotations-2.5.4.jar
jackson-core-2.5.4.jar
jackson-databind-2.5.4.jar
jackson-jaxrs-base-2.5.4.jar
jackson-jaxrs-json-provider-2.5.4.jar
jersey-entity-filtering-2.22.2.jar
jersey-media-json-jackson-2.22.2.jar

【讨论】:

    【解决方案2】:

    这是您的@Produces 和@Consumes 的问题。 @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_JSON) 根据注释,您的端点接收 JSON,结果将是 TEXT。

    但是在您的客户端程序中,您提到内容类型为 json。 conn.setRequestProperty("Content-Type", "application/json");

    因此客户端需要一个 json,而它不是。

    把它改成 conn.setRequestProperty("Content-Type", "text/plain");

    会起作用的。

    【讨论】:

    • 您正在混合客户端和服务器。当服务器@Consumes(MediaType.APPLICATION_JSON) 时,客户端必须设置Content-Type: application/json。当服务器@Produces(MediaType.TEXT_PLAIN)时,客户端必须Accept: text/plain
    • 源服务器拒绝为请求提供服务,因为目标资源上此方法不支持的有效负载格式。
    • 暴露端点的程序产生“文本/计划”的内容。 (带有注释 @Produces 的集合)使用端点的程序需要“application/json”(在您的主程序中设置 setRequestProperty("Content-Type", "application/json"); )
    • Content-Type 用于指定您发送的数据类型,而不是您期望的数据。 “使用端点的程序”,也就是 client,需要以一种格式(由 Content-Type 指定)发送数据, server 期望 receive(由 @Consumes 指定)。
    【解决方案3】:

    看看这个指南:

    我觉得你需要定义一个json处理器:

    https://www.nabisoft.com/tutorials/java-ee/producing-and-consuming-json-or-xml-in-java-rest-services-with-jersey-and-jackson
    

    谢谢。

    【讨论】:

    • 是什么让您认为服务器还没有 JSON 处理器? HTTP 错误代码 415 是 content negotiation 错误,而不是缺少处理器的服务器错误。
    • 正如 OP 上面所说的,问题是找不到处理器,所以我的建议是正确的。不知道为什么我被否决了。
    猜你喜欢
    • 2014-09-15
    • 2018-02-15
    • 2017-07-05
    • 2015-08-29
    • 1970-01-01
    • 2013-05-27
    • 2013-01-16
    • 1970-01-01
    相关资源
    最近更新 更多