我的 Post 请求 returns 在 Jersey 出现 500 内部服务器错误

My Post Request returns a 500 internal server error in Jersey

我正在尝试 post 泽西岛的数据,但我得到的只是一个 500 错误。我的 GET 请求都工作正常。

下面是我的客户端代码。

package main;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

public class MainClass {

    public static void main(String[] args) {


        try {
            Client client = Client.create();
            client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
            WebResource webResource = client
               .resource("http://localhost:7307/mysite/rest_service/postdataclass/postData");

            String input = "{\"name\":\"Violent Soho\",\"last\":\"Jesus Stole My Girlfriend\"}";

            ClientResponse response = webResource.type("application/json")
               .post(ClientResponse.class, input);

            if (response.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                     + response.getStatus());
            }

            System.out.println("Output from Server .... \n");
            String output = response.getEntity(String.class);
            System.out.println(output);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

}

这是我的另一个 class

package service.utils;

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 beans.AASample;

@Path("postdataclass")
public class PostSample {

    @GET
    @Path("/sayHell")
    public Response sayHell() {

        return Response.status(Response.Status.OK).entity("Hell").build();
    }

    @POST
    @Path("/postData")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postData(AASample sample) {

        return Response.status(Response.Status.OK).entity("Profile " + sample.toString()).build();
    }

}

还有我的 AAClass

package beans;

public class AASample {
    private String name;
    private String last;
    public AASample(String name, String last) {
        super();
        this.name = name;
        this.last = last;
    }
    public AASample() {
        super();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLast() {
        return last;
    }
    public void setLast(String last) {
        this.last = last;
    }
    @Override
    public String toString() {
        return "AASample [name=" + name + ", last=" + last + "]";
    }



}

下面是我发出 Post 请求时的服务器日志。

Jul 08, 2016 8:35:22 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 3 * Server has received a request on thread http-nio-8090-exec-4
3 > POST http://localhost:8090/mysite/rest_service/postdataclass/postData
3 > accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
3 > authorization: Basic YWRtaW46YWRtaW4=
3 > connection: keep-alive
3 > content-length: 43
3 > content-type: application/json
3 > host: localhost:8090
3 > user-agent: Java/1.8.0_20

Jul 08, 2016 8:35:23 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Spot Buddy Service] in context with path [/mysite] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.AbstractMethodError: com.fasterxml.jackson.jaxrs.base.ProviderBase._configForReading(Lcom/fasterxml/jackson/databind/ObjectMapper;[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/ObjectMapper;[Ljava/lang/annotation/Annotation;)Lcom/fasterxml/jackson/jaxrs/cfg/EndpointConfigBase;
    at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:644)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:260)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)

我手动将数据转换为 json 对象。将 post 方法更改为

@POST
@Path("/postData")
@Consumes(MediaType.APPLICATION_JSON)
public Response postData(String sample) {

    AASample object = StringToObjectConverter.getObject(sample);
    if(object != null){
         return Response.status(Response.Status.OK).entity("Object " + object.toString()).build();
    }else{
         return Response.status(Response.Status.OK).entity("We Failed Decoding :" + sample +":").build();
    }

}

StringToObjectConverter class

package main
import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import beans.AASample;

public class StringToObjectConverter {


    public static AASample getObject(String postedData) {
        AASample aaSample = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            aaSample = mapper.readValue(postedData, AASample.class);
        } catch (JsonGenerationException e) {
            aaSample = null;
        } catch (JsonMappingException e) {
            aaSample = null;
        } catch (IOException e) {
            aaSample = null;
        }
        return aaSample;
    }


}