如何从java rest api返回json对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26849356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:23:17  来源:igfitidea点击:

how to return json objects from java rest api

javajsonweb-servicesrestjax-rs

提问by Eduardo Dennis

I am trying to return a JSON object from a jax-rs but I get an internal server error whenever I try to access.

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

This is my method, I am using javax.ws.rs.GET.

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

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

This is the class:

这是课程:

public class Testy {

    private final String value;

    public Testy(String value){

        this.value = value;
    }

  public String getValue(){

      return value;
   }

}

My Pom.xml has this dependency, I have tried various dependencies, but none work. There are various maven resources for jersey, there's jersey-client jersey-core.

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

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

I am using Glassfish 4.

我正在使用 Glassfish 4。

Questions about working with Jersey:

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

I have seen some placeswhere they mention you need to have initialize POJO support, it seems like its for jersey 1.* but I am not sure. I don't need this if I am using 2.* ?

我看过一些地方,他们提到您需要初始化 POJO 支持,这似乎是针对球衣 1.* 的,但我不确定。如果我使用 2.* ,我不需要这个?

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

我是否必须修改 web.xml 以指向球衣 servlet?

How can I produce and consume JSON objects using POJO classes !?

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

Edit

编辑

Here is my auto generated config class.

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

enter image description here

在此处输入图片说明

采纳答案by Eduardo Dennis

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

我必须将 HymansonFeature 资源添加到我的 ApplicationConfig 类中。

    @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.Hymanson.HymansonFeature.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);
    }

}

Then everything worked!

然后一切正常!

Note

笔记

When I used the com.fasterxml.Hymanson.jaxrs dependency I was unable to deploy my application. I started getting a WELD-001408 Unsatisfied dependencies for typeerror so I had to exclude the jersey media multi part.

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

These are the dependencies in my pom.xml

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

 <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-Hymanson</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.Hymanson.jaxrs</groupId>
        <artifactId>Hymanson-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>

回答by Lokesh

回答by Scott Johnson

It may be that you need to expose the value property of your Testy object with a getter.

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