Jersey 415 不支持的媒体类型 [英] Jersey 415 Unsupported Media Type

查看:41
本文介绍了Jersey 415 不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几个小时以来一直在尝试纠正 http 错误 415 Unsupported Media Type 但它仍然显示媒体不受支持的页面.我在 Postman 中添加标题 application/json.

I have been trying since hours to correct http error 415 Unsupported Media Type but it is still showing media unsupported page. I am adding headers application/json in Postman.

这是我的Java代码

package lostLove;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;  
import javax.ws.rs.POST;
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; 

import org.json.JSONObject;


@Path("/Story") 
public class Story {

      @POST
      @Consumes({"application/json"})
      @Produces(MediaType.APPLICATION_JSON)
    //  @Consumes(MediaType.APPLICATION_JSON)
    //  @Path("/Story") 
      public JSONObject sayJsonTextHello(JSONObject inputJsonObj) throws Exception {

        String input = (String) inputJsonObj.get("input");
        String output = "The input you sent is :" + input;
        JSONObject outputJsonObj = new JSONObject();
        outputJsonObj.put("output", output);

        return outputJsonObj;
      }

      @GET  
      @Produces(MediaType.TEXT_PLAIN)  

      public String sayPlainTextHello() {  
        return "hello";
      }

}

这是我的 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LostLove</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>  
        <param-name>jersey.config.server.provider.packages</param-name>  
        <param-value>lostLove</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>
</web-app>

推荐答案

我们的对象如何在响应流和请求流之间进行序列化和反序列化,是通过 MessageBodyWritersMessageBodyReaders.

How our objects are serialized and deserialized to and from the response stream and request stream, is through MessageBodyWriters and MessageBodyReaders.

将会发生的是,将从提供者的注册表中进行搜索,寻找可以处理 JSONObject 和媒体类型 application/json 的提供者.如果找不到,则 Jersey 无法处理该请求,并将发送 415 Unsupported Media Type.您通常也应该在服务器端记录异常.不确定您是否有机会查看日志.

What will happens is that a search will be done from the registry of providers, for one that can handle JSONObject and media type application/json. If one can't be found, then Jersey can't handle the request and will send out a 415 Unsupported Media Type. You should normally get an exception logged also on the server side. Not sure if you gotten a chance to view the log yet.

Jersey 没有任何用于 org.json 对象的标准读取器/写入器.您必须在网络上搜索实现或自己编写一个,然后注册它.您可以在此处阅读有关如何实施它的更多信息.

Jersey doesn't have any standard reader/writer for the org.json objects. You would have to search the web for an implementation or write one up yourself, then register it. You can read more about how to implement it here.

或者,您可以接受一个字符串并返回一个字符串.只需构造带有字符串参数的JSONObject,并在返回时调用JSONObject.toString().

Alternatively, you could accept a String and return a String. Just construct the JSONObject with the string parameter, and call JSONObject.toString() when returning.

@POST
@Consumes("application/json")
@Produces("application/json")
public String post(String jsonRequest) {
    JSONObject jsonObject = new JSONObject(jsonRequest);
    return jsonObject.toString();
}

我的建议是使用像 Jackson 这样的数据绑定框架,它可以处理与模型对象(简单 POJO)之间的序列化和反序列化.例如你可以有一个像

My suggestion instead would be to use a Data binding framework like Jackson, which can handle serializing and deserializing to and from out model objects (simple POJOs). For instance you can have a class like

public class Model {
    private String input;
    public String getInput() { return input; }
    public void setInput(String input) { this.input = input; }
} 

您可以将 Model 作为方法参数

You could have the Model as a method parameter

public ReturnType sayJsonTextHello(Model model)

ReturnType 也一样.只需为您想要返回的类型创建一个 POJO.JSON 属性基于 JavaBean 属性名称(遵循上述命名约定的 getter/setter).

Same for the ReturnType. Just create a POJO for the type you wan to return. The JSON properties are based on the JavaBean property names (getters/setters following the naming convention shown above).

要获得此支持,您可以添加此 Maven 依赖项:

To get this support, you can add this Maven dependency:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.17</version>  <!-- make sure the jersey version 
                                  matches the one you are using -->
</dependency>

或者,如果您没有使用 Maven,您可以查看这篇博文,对于可以独立下载的 jar.

Or if you are not using Maven, you can see this post, for the jars you can download independently.

一些资源:

这篇关于Jersey 415 不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆