我的帖子请求在Jersey中返回500内部服务器错误

时间:2016-07-08 18:08:25

标签: rest jersey jersey-2.0 jersey-client

我正试图在泽西岛发布数据,但我得到的是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();
        }

    }

}

这是我的另一个班级

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 + "]";
    }



}

以下是我发布帖子请求时的服务器日志。

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)

1 个答案:

答案 0 :(得分:0)

我手动将数据转换为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类

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;
    }


}