泽西2.x不支持POJO到json? [英] Jersey 2.x does not support POJO to json?

查看:74
本文介绍了泽西2.x不支持POJO到json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用jersey.i将java对象转换为json对象时,我有一个例外。

While i am trying to convert java object to json object by using jersey.i am having an exception.

Java代码:-( POJO)

Java code :-(POJO)

  package org.jersey.media.type;
  import javax.xml.bind.annotation.XmlRootElement;
  @XmlRootElement
  public class MyJaxbBean {
  public String name;
  public int age;

  public MyJaxbBean() {} // JAXB needs this

  public MyJaxbBean(String name, int age) {
    this.name = name;
    this.age = age;
    }
 }

资源: -

package org.jersey.media.type;
import javax.ws.rs.GET;
import javax.ws.rs.Path; 
import javax.ws.rs.Produces;
@Path("/media")
public class MediaResources {
@GET
@Produces("application/xml")
public MyJaxbBean getMyBean() {
    return new MyJaxbBean("Agamemnon", 32);
 }
}

例外: -

Sep 05, 2015 4:20:38 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWr   iterInterceptor aroundWriteTo
    SEVERE: MessageBodyWriter not found for media type=application/json,            type=class org.jersey.media.type.MyJaxbBean, genericType=class     org.jersey.media.type.MyJaxbBean.

部署描述符: -

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>JerseyAuthentication</display-name>
<servlet>
    <servlet-name>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.jersey.media.type</param-value>
    </init-param>
    <!-- <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.jackson.JacksonFeature,org.glassfish.jersey.jackson.JacksonBinder</param-value>
    </init-param> -->
</servlet>
<servlet-mapping>
    <servlet-name>Application</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>login.html</welcome-file>
</welcome-file-list>
  </web-app>

依赖关系: -

我不是在使用maven,请帮助我解决这个问题

I am not using maven ,kindly some one help me to resolve this

推荐答案

是的如此 Jersey发布不附带任何JSON / POJO支持。你需要自己找到它们并将它们添加到你的项目中。

Yeah so the Jersey distribution doesn't come with any JSON/POJO support. You need to find them yourself and add them to your project.

如果你想使用 JacksonFeature ,那么你需要抓住所有的罐子。您可以在提供程序类参数中指定所需的任何类,但如果您实际上没有它,则不会发生任何事情。除了任何例外情况,你都不会被告知。

If you want to use the JacksonFeature, then you need to grab all the jars for it. You can specify any class you want in the provider classes param, but if you don't actually have it, nothing will happen. You are not even told by any exceptions.

这些都是你需要的罐子


  • jersey-media-json- jackson-2.17

  • jersey-entity-filtering-2.17

  • jackson-jaxrs-json-provider-2.3.2

  • jackson-core-2.3.2

  • jackson-databind-2.3.2

  • jackson-annotations-2.3.2

  • jackson-jaxrs-base-2.3.2

  • jackson-module-jaxb-annotations-2.3.2

  • jersey-media-json-jackson-2.17
  • jersey-entity-filtering-2.17
  • jackson-jaxrs-json-provider-2.3.2
  • jackson-core-2.3.2
  • jackson-databind-2.3.2
  • jackson-annotations-2.3.2
  • jackson-jaxrs-base-2.3.2
  • jackson-module-jaxb-annotations-2.3.2

请注意,这些都是用于Jersey 2.17的所有罐子。如果您使用的是较新版本,那么您应该去这里,然后选择您正在使用的Jersey版本。你可以下载它。然后搜索具有相同版本的实体过滤罐。

Note that these are all the jars that are used for Jersey 2.17. If you are using a newer version, then you should go here, and select the version of Jersey you are using. You can download it. Then search for the entity-filtering jar with the same version.

所有其他杰克逊罐子的版本相同,但你应该看看杰克逊的哪个版本 jersey-media-json-jackson 使用。当您单击Jersey版本时,您应该能够向下滚动以查看它使用的Jackson版本。然后开始搜索该版本的所有上述Jackson罐子。

All the other Jackson jars are of the same version, but you should see which version of Jackson the jersey-media-json-jackson uses. When you click the Jersey version, you should be able to scroll down to see which Jackson version it uses. Then just start searching for all the above Jackson jars for that version.

例如,如果您选择最新的Jersey 2.21版 jersey-media-json-jackson ,您可以向下滚动并查看它使用的是Jackson 2.5.4。所以你应该在2.5.4而不是2.3.2中搜索上面所有的Jackson罐子,如上所示。

For instance, if you select the latest Jersey 2.21 version of the jersey-media-json-jackson, you can scroll down and see that it uses Jackson 2.5.4. So you should search for all the above Jackson jars in 2.5.4 instead of the 2.3.2 as shown above.

对于Maven用户的注意事项,您只需要

Note for Maven users, all you need is

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

此依赖关系将拉入您在上面列出的所有罐子。

This dependency will pull in all the jars you see listed above.

这篇关于泽西2.x不支持POJO到json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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