【问题标题】:how to return json objects from java rest api如何从java rest api返回json对象
【发布时间】:2014-11-10 17:22:23
【问题描述】:

我正在尝试从 jax-rs 返回一个 JSON 对象,但每当我尝试访问时都会收到内部服务器错误。

这是我的方法,我用的是javax.ws.rs.GET

@GET
@Produces("application/json")
public Testy getJson() {
   return new Testy("hello");
}

这是课程:

public class Testy {

    private final String value;

    public Testy(String value){

        this.value = value;
    }

  public String getValue(){

      return value;
   }

}

我的 Pom.xml 有这个依赖项,我尝试了各种依赖项,但都没有工作。 jersey有各种maven资源,有jersey-client jersey-core。

<dependency>
 <groupId>com.fasterxml.jackson.jaxrs</groupId>
 <artifactId>jackson-jaxrs-json-provider</artifactId>
 <version>2.3.3</version>
</dependency>

我正在使用 Glassfish 4。

有关与泽西岛合作的问题:

我见过一些places,他们提到您需要初始化 POJO 支持,这似乎适用于球衣 1.*,但我不确定。如果我使用 2.*,我不需要这个?

Do I have to modify the web.xml to point to the jersey servlet ?

如何使用 POJO 类生成和使用 JSON 对象!?

编辑

这是我自动生成的配置类。

【问题讨论】:

    标签: java json web-services rest jax-rs


    【解决方案1】:

    I had to add the JacksonFeature resource to my ApplicationConfig Class.

        @javax.ws.rs.ApplicationPath("webresources")
    public class ApplicationConfig extends Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> resources = new java.util.HashSet<>();              
           addRestResourceClasses(resources);
           resources.add(org.glassfish.jersey.jackson.JacksonFeature.class);
            return resources;
        }
    
        /**
         * Do not modify addRestResourceClasses() method.
         * It is automatically populated with
         * all resources defined in the project.
         * If required, comment out calling this method in getClasses().
         */
        private void addRestResourceClasses(Set<Class<?>> resources) {
            resources.add(com.wfscorp.restapitwo.GenericResource.class);
        }
    
    }
    

    然后一切正常!

    注意

    当我使用 com.fasterxml.jackson.jaxrs 依赖项时,我无法部署我的应用程序。我开始收到WELD-001408 Unsatisfied dependencies for type 错误,所以我不得不排除球衣媒体多部分。

    这些是我的 pom.xml 中的依赖项

     <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.13</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency> 
    
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.4.0</version>
        </dependency>
    
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.7</version>
            <scope>provided</scope>
        </dependency>
    
    </dependencies>
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 需要按照示例中的说明创建 Jersey 客户端。
      【解决方案3】:

      您可能需要使用 getter 公开 Testy 对象的 value 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多