简体   繁体   中英

HTTP 500 Internal Server Error using Jersey

I am using Jersey. Therefore I have a issue when I try to get a JSON object response (only with JSON) from my service. I'm using a simple apache localhost to host my service.

service

@Path("/profile")
public class ProfileDataService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ProfileBean getProfileData() {
        ProfileBean profile = fillProfileInstance();

        return profile;
    }
}

web.xml

<web-app ...>
    <display-name>cinemak-service</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <!-- Register resources and providers under com.vogella.jersey.first package. -->
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.cinemak.service.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

client

public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);

    WebTarget target = client.target(getURIBase());

    System.out.println(target.path("rest").path("profile").request().accept(MediaType.APPLICATION_JSON).get(String.class));

}

//Build and returns the specific uri base for cinemak-service
private static URI getURIBase() {
    return UriBuilder.fromUri("http://localhost:8080/cinemak-service").build();
}

And the error I get:

Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1002)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:799)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:687)
    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:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:683)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:411)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:307)
    at com.cinemak.client.ProfileDataClient.main(ProfileDataClient.java:21)

It looks like your server is not responding... I guess your client java code are in different java project , right? If not, if your client java code is in the same java project you must start your application server, after that, you can run your main method of your client class. If your client java code is in other java project, is the same, you must startup your server to be able to call the RestFul path. By the way, your Restful application project name is "cinemak-service" in lower cases? Just to be sure that is not a typo.

Bellow worst coding you will get HTTP 500 Internal Server Error using Jersey.

  • These example two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will
    always fail.
package rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/HelloService")
public class HelloService {
    @GET
    @Path("/{param}/{param2}")
    public Response getMsg(@PathParam("param") String msg,
            @PathParam("param2") String msg2) { // from path
        String output = "Jersey say : " + msg + "/" + msg2;
        return Response.status(200).entity(output).build();
    }

    @GET
    @Path("/{param}/{param2}")
    public Response getMsg2(@PathParam("param") String msg,
            @PathParam("param2") String msg2) { // from path
        String output = "Jersey say-2 : " + msg + "/" + msg2;
        return Response.status(200).entity(output).build();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM