java – Jersey JSON序列化

我设置了一个eclipse WebApp项目,并将Jersey和Jackson JAR放在WEB-INF / lib目录中.我想使用JSON序列化,但没有设法修复此错误:

MessageBodyWriter not found for media type=application/json, type=class com.rest.Greeting, genericType=class com.rest.Greeting. I already googled much but all the solutions are updated or don't solve my issue. Here is the structure of my project:

WEB-INF / lib文件夹包含以下JAR:

aopalliance-repackaged-2.3.0-b10.jar
asm-debug-all-5.0.2.jar
cglib-2.2.2.jar
datafactory-0.8.jar
hk2-api-2.3.0-b10.jar
hk2-locator-2.3.0-b10.jar
hk2-utils-2.3.0-b10.jar
jackson-annotations-2.4.0.jar
jackson-core-2.4.0.jar
jackson-databind-2.4.0.jar
jackson-jaxrs-base-2.4.0.jar
jackson-jaxrs-json-provider-2.4.0.jar
jackson-module-jaxb-annotations-2.4.0.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.3.0-b10.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-guava-2.13.jar
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar

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>Rest Test</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>Greeting</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.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Greeting</servlet-name>
    <url-pattern>/greet/*</url-pattern>
  </servlet-mapping>
</web-app>

Greeting.java:

@XmlRootElement
@Path("/greeting")
public class Greeting {

    private String greeting;

    public Greeting() {
        this.setGreeting("hello");
    }

    @GET
    @Produces(MediaType.TEXT_XML)   
    public Greeting sayHello() {
        return new Greeting();
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}

WebApp项目在嵌入式Tomcat v7服务器中运行(在eclipse中查看“Servers”).
请求:

http://localhost:8080/com.rest/greet/greeting

只要我使用MediaType.TEXT_XML就没有错误,内容如下所示:

<greeting>
<greeting>hello</greeting>
</greeting>

当我将MediaType更改为“MediaType.APPLICATION_JSON”时,将引发以下异常(如上所述).我已经注意到它取决于注释“@XmlRootElement”但是没有类似“JsonRootElement”的东西,并且jackson / genson API声称它开箱即用(由于自动注册机制).你知道怎么解决它吗?

非常感谢每一位帮助!
谢谢
golbie

解决方法:

您只需注册JacksonJsonProvider即可.有很多方法可以实现这一目标:

>在web.xml中明确注册JacksonJsonProvider:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider</param-value>
</init-param>

要么

>在web.xml中注册扩展javax.ws.rs.core.Application的类

<init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.rest.MyApplication</param-value>
</init-param>

然后执行应用程序类中的所有配置:

package com.rest;

import org.glassfish.jersey.server.ResourceConfig;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

public class MyApplication extends ResourceConfig {

    public MyApplication() {
        packages("com.rest");
        register(JacksonJsonProvider.class);
    }

ResourceConfig是javax.ws.rs.Application的子类,它为您提供了一些帮助您轻松注册的辅助方法.

要么

>使用自动注册.只需将依赖项添加到jersey-media-json-jackson即可

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.13</version>
</dependency>

不过要小心.它将注册超过您需要的:

> JacksonJaxbJson提供者,
> JsonParseExceptionMapper,
> JsonMappingExceptionMapper

看看source code看看它的作用.

上一篇:java – com.fasterxml.jackson.core.JsonParseException:意外的字符


下一篇:RESTful Python for Java(Jersey)开发人员