使用Jersey和Jackson消耗JSON请求

时间:2014-07-25 17:13:19

标签: json jersey jackson

我正在开发一个基本的Jersey WebService,它将接受JSON请求并以JSON方式发回数据。我还开发了一个基本的测试客户端来发送请求并作为Java Standalone程序获得响应。我能够成功发送并获得GET和POST请求(text / html)的响应,但是当我发送JSON数据时,我得到以下错误。它似乎与杰克逊翻译有关。我没有使用maven。

泽西岛服务:

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.xxx.inventory.bean.XXXRequestBean;

@Path("UserInfoService")
public class XXXXService {


@GET
@Path("/name/{i}")
@Produces(MediaType.TEXT_XML)
public String userName(@PathParam("i") String i) {

   System.out.println(" ********************* Web Service Request GET ************************* "); 
   String name = i;
   return "<User>" + "<Name>" + name + "</Name>" + "</User>";
}

//PROBLEM HERE *********
@Path("/inventory/")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
public String getInventoryRequest(InventoryRequestBean invbean)
{
    System.out.println(" Welcome to Bean Matach :"+invbean.toString());

    return ("<html>Weclcome</html>");

}


@Path("/inventoryCheck/")
@POST
@Consumes(MediaType.TEXT_HTML)
@Produces(MediaType.TEXT_HTML)
public String getInventoryRequestString(String test)
{
     System.out.println(" Welcome to Bean String Test :"+test);

      return ("<html>Weclcome</html>");

  }


}

Bean:

 import java.io.Serializable;

 import javax.xml.bind.annotation.XmlRootElement;

 import com.fasterxml.jackson.annotation.JsonProperty;

 @XmlRootElement
 public class XXXRequestBean implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
@JsonProperty
String urlString;
@JsonProperty
String productCd;
@JsonProperty
String systemCd;
@JsonProperty
String response;
@JsonProperty
int systemId;
@JsonProperty
int timeOut;

public XXXRequestBean(String urlString,String productCd,String systemCd,String response,int systemId,int timeOut)
{
    this.urlString = urlString;
    this.productCd = productCd;
    this.systemCd = systemCd;
    this.response = response;
    this.systemId = systemId;
    this.timeOut = timeOut;


}

public XXXRequestBean()
{

}

@Override
public String toString()
{
    System.out.println("{\"urlString\":\""+this.urlString+"\",\"productCode\":\""+this.productCd+"\",\"systemCode\":\""+this.systemCd+"\",\"response\":\""+this.response+"\",\"systemId\":"+this.systemId+",\"timeOut\":"+this.timeOut+"}");

    return new StringBuffer("{\"urlString\":\""+this.urlString+"\",\"productCode\":\""+this.productCd+"\",\"systemCode\":\""+
                    this.systemCd+"\",\"response\":\""+this.response+"\",\"systemId\":"+this.systemId+",\"timeOut\":"+this.timeOut+"}").toString();
}

}

客户端:

 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import com.XXX.inventory.bean.XXXRequestBean;

 public class JerseyClient {



public static void main(String[] args) {
    try {
        simpleWeb();
        Client client = ClientBuilder.newClient();
        InventoryRequestBean ivb = new InventoryRequestBean("Test","test2","welcome","",1,2);
        WebTarget serviceWeb = client.target("http://localhost:8080/XXXService/UserInfoService/inventory/");
        Response response = serviceWeb.request().post(Entity.entity(ivb.toString(),MediaType.APPLICATION_JSON));
        System.out.println(response.getStatus());
        System.out.println(response.readEntity(String.class));

    } catch (Exception ex) {
        Logger.getLogger(JerseyClient.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public static void simpleWeb()
{

    Client client1 = ClientBuilder.newClient();
    InventoryRequestBean ivb = new InventoryRequestBean("Test","test2","welcome","",1,2);
    WebTarget serviceWeb1 = client1.target("http://localhost:8080/XXXService/UserInfoService/XXXCheck/");
    Response response1 = serviceWeb1.request().post(Entity.entity(ivb.toString(),MediaType.TEXT_HTML));
    System.out.println(response1.getStatus());
    System.out.println(response1.readEntity(String.class));

}


public static void simpleWeb1()
{

    Client client1 = ClientBuilder.newClient();
    WebTarget serviceWeb1 = client1.target("http://localhost:8080/XXXService/UserInfoService/name/xxuser");
    Response response1 = serviceWeb1.request().get();
    System.out.println(response1.getStatus());
    System.out.println(response1.readEntity(String.class));

}

}

SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path          [/XXXService] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.AbstractMethodError: com.fasterxml.jackson.jaxrs.base.ProviderBase._configForReading(Lcom/fasterxml/jackson/databind/ObjectReader;[Ljava/lang/annotation/Annotation;)Lcom/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase;] with root cause
 java.lang.AbstractMethodError: com.fasterxml.jackson.jaxrs.base.ProviderBase._configForReading(Lcom/fasterxml/jackson/databind/ObjectReader;[Ljava/lang/annotation/Annotation;)Lcom/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase;
at com.fasterxml.jackson.jaxrs.base.ProviderBase._configForReading(ProviderBase.java:468)
at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:765)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:251)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:229)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:72)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
at org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:270)
at org.glassfish.jersey.server.internal.inject.EntityParamValueFactoryProvider$EntityValueFactory.provide(EntityParamValueFactoryProvider.java:96)
at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:81)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:121)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:387)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:331)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:103)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:269)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:252)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1023)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

使用

的罐子列表
/InventoryService/WebContent/WEB-INF/lib/jackson-annotations-2.4.1.jar
/InventoryService/WebContent/WEB-INF/lib/jackson-core-2.4.1.jar
/InventoryService/WebContent/WEB-INF/lib/jackson-databind-2.4.1.jar
/InventoryService/WebContent/WEB-INF/lib/jackson-jaxrs-base-2.4.1.jar
/InventoryService/WebContent/WEB-INF/lib/jackson-jaxrs-json-provider-2.2.0.jar
/InventoryService/WebContent/WEB-INF/lib/javassist-3.18.1-GA.jar
/InventoryService/WebContent/WEB-INF/lib/javax.annotation-api-1.2.jar
/InventoryService/WebContent/WEB-INF/lib/javax.inject-2.3.0-b05.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-client-2.8.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-common-2.8.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-container-grizzly2-http-2.8.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-container-jdk-http-2.8.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-container-servlet-core-2.8.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-guava-2.8.jar
/InventoryService/WebContent/WEB-INF/lib/jersey-media-json-processing-2.8.jar

2 个答案:

答案 0 :(得分:2)

我在列出罐子后想到了这一点,将jackson-jaxrs-json-provider-2.2.0.jar更改为2.4.1解决了这个问题。

还注意到toString方法中JSON构造中的其他几个错误,字段名称在几个地方都是错误的。

答案 1 :(得分:1)

遇到同样的问题,在我的例子中,它通过maven依赖管理系统将实现作为依赖关系添加到pom.xml中的客户端来帮助修复java.lang.AbstractMethodError:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.2.2</version>
        </dependency>