gpt4 book ai didi

java - REST API Jersey Glassfish 不序列化 json

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:59 25 4
gpt4 key购买 nike

我正在使用 jersey glassfish 2.0 实现与 tomcat 8 一起实现 Restfull Web 服务。

我的网络服务工作正常,但我的 java bean 的序列化不正确。例如我的网络服务返回遵循 json

{
"errorCode": 200,
"obj": "DestinationCountry [img=[], code=3, video=AbstractMultiMediaContent [id=0, name=null, url=null, file=null, contentType=null, comments=null, owner=null, likes=null, tags=null, is=null], guide=Guide [guideTraslation=null, getPoi()=null, ......
}

正确的 json 应该是

{
"errorCode": 200,
"errorMsg": null,
"obj": [
{
"comparableInfo": null,
"id": 2195,
"code": "3",
"name": "Sudafrica",
"continent": null,
"idDestinationCountry": 690,
"previewImage": {
"comparableInfo": null,
"id": 0,
"name": null,
"url": "sud_africa_viaggi_diving_2",
"file": null,
"contentType": null,
"comments": null,
"owner": null,
"likes": null,
"tags": null,
"is": null,
"numberOfLike": 0
}

, …… ......

这是我的java代码

@GET
@Path("/guide/id/{idguide}")
@Produces(MediaType.APPLICATION_JSON)
public Response getGuideById(@PathParam("idguide") int idguide, @QueryParam("uuid") String uuid)
{

Response response = null ;
DCResponse<Guide> dcResp = null ;
Configuration conf = ConfigurationFactory.getInstance() ;
try
{
conf.configurationLoader(UtilsConstants.SYS_CONF_FILE);
DiveCircleManagerIV2 pm = (DiveCircleManagerIV2) CoreFactory.getInstance(conf.getProperty(DiveCircleConstants.DIVECIRCLE_MANAGER)) ;
dcResp = pm.findGuideById(idguide) ;
response = Response.status(dcResp.getErrorCode()).entity(dcResp).build() ;
}
catch (ConfigurationException | IOException | FactoryException | ManagerException e) {
log.error(e);
e.printStackTrace();
response = Response.status(DiveCircleConstants.INTERNAL_ERROR_CODE).build() ;
}

return response ;

}

这是我的 pom 依赖项

<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.2.8</version>
</dependency>

<dependency>
<groupId>com.divecircle.booking.api</groupId>
<artifactId>BookingAPI</artifactId>
<version>0.1</version>
</dependency>

<dependency>
<groupId>com.geeoz.ean</groupId>
<artifactId>ean-api</artifactId>
<version>3.25.0d1</version>
</dependency>


<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>


<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>


<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>

<dependency>
<groupId>com.cloudinary</groupId>
<artifactId>cloudinary</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.mandrillapp.wrapper.lutung</groupId>
<artifactId>lutung</artifactId>
<version>0.0.4</version>
</dependency>

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.5</version>
</dependency>

<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>



<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey.version}</version>
</dependency>

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

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>

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


<!-- ###############################################-->
<!-- <dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>${jersey.version}</version>
</dependency>

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

<!-- ###############################################-->



<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.0</version>
</dependency>



<dependency>
<groupId>com.cstadvising.utils</groupId>
<artifactId>Utils</artifactId>
<version>1.0</version>
</dependency>


<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>



<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.2.8</version>
</dependency>




<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.11</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>

这是我的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>DiveCircle</display-name>




<listener>
<description>System Inizilizer</description>
<listener-class>com.cstadvising.utils.core.listener.SystemContextListener</listener-class>
</listener>

<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.moxy.json.MoxyFeature;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.divecircle.restful</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>



<listener>
<listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>

<context-param>
<param-name>quartz:config-file</param-name>
<param-value>/quartz.properties</param-value>
</context-param>
<context-param>
<param-name>quartz:shutdown-on-unload</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:wait-on-shutdown</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:start-on-load</param-name>
<param-value>true</param-value>
</context-param>
<!--
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet
</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
-->
</web-app>

有什么建议或解决方案吗?

最佳答案

问题出在 MOXy(即 jersey-media-moxy )。序列化时它需要知道所有类型信息。与DCResponse ,你有一个泛型类型,它在编译时被删除

@XmlRootElement
public class DCResponse<T> {

private int status;
private String errorMsg;
private List<T> obj;

DCResponse<Guide> dcResp编译后,类型被删除,因为 type erasure 。为了解决这个问题, Jersey 提供了 GenericEntity ,它捕获泛型类型,并将其传递给序列化器。所以你会这样做

DCResponse<Guide> dcResp = dcResp = pm.findGuideById(idguide);
response = Response.status(dcResp.getErrorCode())
.entity(new GenericEntity<DCResponse<Guide>>(dcResp) {}).build();

这应该有效。

或者,如果您不想担心此类问题,请考虑使用 Jackson,而不是 MOXy。 Jackson 一般不会遇到这样的问题,因为它只是内省(introspection)属性。要使用 Jackson,您需要摆脱 MOXy(这很重要),并添加以下内容

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

然后代替 MoxyJsonFeature ,您需要注册 JacksonFeature

关于java - REST API Jersey Glassfish 不序列化 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35561392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com