如何从Java Rest API返回json对象 [英] how to return json objects from java rest api
问题描述
我正在尝试从jax-rs返回JSON对象,但是每当尝试访问时都会收到内部服务器错误.
I am trying to return a JSON object from a jax-rs but I get an internal server error whenever I try to access.
这是我的方法,我正在使用javax.ws.rs.GET
.
This is my method, I am using 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具有此依赖关系,我尝试了各种依赖关系,但没有任何作用.球衣有各种Maven资源,其中有jersey-client jersey-core.
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.
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.3</version>
</dependency>
我正在使用Glassfish 4.
I am using Glassfish 4.
有关使用Jersey的问题:
我已经看到一些位置,其中提到您需要进行初始化POJO支持,似乎适用于1. *版球衣,但我不确定.如果我使用的是2. *,我不需要这个吗?
I have seen some places where 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.* ?
我是否必须修改web.xml以指向jersey servlet?
如何使用POJO类生成和使用JSON对象!
这是我自动生成的配置类.
Here is my auto generated config class.
推荐答案
我必须将JacksonFeature资源添加到我的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.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
错误,因此不得不排除球衣媒体的多个部分.
When I used the com.fasterxml.jackson.jaxrs dependency I was unable to deploy my application. I started getting a WELD-001408 Unsatisfied dependencies for type
error so I had to exclude the jersey media multi part.
这些是我pom.xml中的依赖项
These are the dependencies in my 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>
这篇关于如何从Java Rest API返回json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!