获得Jersey 2.x POJO JSON支持与Jetty一起工作

时间:2014-05-29 03:47:04

标签: json rest jetty jersey-2.0

我是新的RESTful网络服务,并且一直在使用Jersey和Heroku(使用Jetty堆栈)。我正在编写一个简单的REST API,它在JSON中为Map<String,String>请求返回GET

然而,我遇到了500恐怖事件。错误消息是:

  

HTTP状态500 - org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application / json的MessageBodyWriter,type = class java.util.LinkedHashMap,genericType = java.util.HashMap。

以下是我资源的代码段:

@GET
@Produces("application/json")
public HashMap<String,String> getIt() {
    HashMap<String,String> nameValue = new LinkedHashMap<String,String>();
    nameValue.put("Alpha","One");
    return nameValue;
}

以下是我的web.xml文件:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>org.example.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

这是我的主要课程

public class Main {

    public static void main(String[] args) throws Exception{
        // The port that we should run on can be set into an environment variable
        // Look for that variable and default to 8080 if it isn't there.
        String webPort = System.getenv("PORT");
        if (webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }

        final Server server = new Server(Integer.valueOf(webPort));
        final WebAppContext root = new WebAppContext();

        root.setContextPath("/");
        // Parent loader priority is a class loader setting that Jetty accepts.
        // By default Jetty will behave like most web containers in that it will
        // allow your application to replace non-server libraries that are part of the
        // container. Setting parent loader priority to true changes this behavior.
        // Read more here: http://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading
        root.setParentLoaderPriority(true);

        final String webappDirLocation = "src/main/webapp/";
        root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
        root.setResourceBase(webappDirLocation);


        server.setHandler(root);

        server.start();
        server.join();
    }
}

即使浏览了之前的Stackoverflow答案,例如thisthis,我找不到解决问题的方法,因为他们没有使用Jetty解决Jersey 2.x问题。我已将以下内容添加到我的pom.xml文件中,但问题仍然存在,因为无法向Jetty服务器注册JSON绑定。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
</dependency>

2 个答案:

答案 0 :(得分:4)

由moxy自动注册提供商并不像Jersey Reference所述那样工作。

根据他们所说的,只有moxy和jackson具有POJO到JSON转换功能。

文件说杰克逊没有自动注册(不是任何问题!)

1。在POM.XML中与Jackson交换Moxy

删除:

<dependency>
 <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-moxy</artifactId>
  <version>${jersey.version}</version>
</dependency>

添加:

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>${jersey.version}</version>
</dependency>

2。注册Jackson Message Body读者和作家:

org.codehaus.jackson.jaxrs添加到提供程序包列表中。这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>Web app name</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.myorg.myproj.api;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Web app name</servlet-name>
    <url-pattern>/v1/*</url-pattern>
</servlet-mapping>


P.S。
我不是在推广jackson,只是moxy没有为我工作,它的作者没有像他们做广告那样自动注册,也无法找到有关手动注册的文档!

答案 1 :(得分:2)

如果使用杰克逊实施

根据Jersey Documentation - 请注意,Jackson 1.x(org.codehaus.jackson)和Jackson 2.x(com.fasterxml.jackson)之间的名称空间存在差异。

如果您使用的是jackson 2.x,则需要将 com.fasterxml.jackson.jaxrs 作为init param注册到web.xml中的ServletContainer,如下所示:

<servlet>
    <servlet-name>RESTServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.fasterxml.jackson.jaxrs</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>