本文介绍了获得Jersey 2.x POJO JSON支持以与Jetty一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new RESTful web services and have been playing around with Jersey and Heroku (which uses a Jetty stack). I'm writing a simple REST API which returns a Map<String,String> in JSON for a GET request.

但是我遇到了500错误.错误消息是:

I'm however running into a 500 eror. The error message is :

以下是我的资源的代码段:

Below is the code snippet for my resource :

@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答案,例如,我找不到解决我的问题的方法,因为它们无法使用Jetty解决Jersey2.x.我已将以下内容添加到我的pom.xml文件中,但是由于无法在Jetty服务器上注册JSON绑定,问题仍然存在.

Even after browsing through previous Stackoverflow answers like this or this, I could not find a way to solve my problem as they do not address Jersey 2.x with Jetty. I've added the following to my pom.xml file, however the problem still persists as unable to register the JSON bindings with the Jetty server.

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

推荐答案

moxy无法自动注册提供者,如《泽西岛参考》 .

Automatic registration of providers by moxy didn't work as stated by Jersey Reference.

按照他们所说,只有moxy和jackson具有POJO到JSON转换功能.

As per what they have stated, only moxy and jackson has POJO to JSON conversion feature.

文档显示Jackson不会自动注册(无论如何都没有问题!)

Documentation says Jackson doesn't auto register(Not a problem any way!)

删除:

<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邮件正文读者和作家:

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

2. Register Jackson Message Body Readers and Writers :

Add org.codehaus.jackson.jaxrs to provider packages list. Here is my 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对我不起作用,它的作者由于广告宣传而无法自动注册,并且找不到有关手动注册的文档!

P.S.
I am not promoting jackson, just that moxy didn't work for me, its writers failed to auto register as they advertised and could not find documentation about manual registration!

这篇关于获得Jersey 2.x POJO JSON支持以与Jetty一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:51