HTTP 500 Internal server error in a simple REST-based program. Confused in GET and POST while receiving / sending a response from the server

I am implementing the basic client server architecture using REST services for the first time. This time I complicated the task of including several classes and services using class objects as parameters between the client and server. I am running a server on ApacheTomcat7. It succeeds. When I start my client, it gives me an error: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error I tried to debug my code, it looks like I'm receiving / sending the answer incorrectly. I know his unreasonable idea to share all classes here, but I have no choice, since he spent a lot of time. Any help would be appreciated. Thanks in advance.

Below is my ImageProgress class. This class is present both on the server and on the client.

 @XmlRootElement public class ImageProgress{ private String name; public ImageProgress( String image_name){ this.name = image_name; } public String getName() { return name; } public void setName( String name ){ this.name = name; } } 

HPCResponse is a class whose object will be returned to the client as a server response. HPCResponse will basically return an ImageProgress object that will give me the intended result.

 @XmlRootElement public class HPCResponse { private ImageProgress imgProgress; public ImageProgress getImgProgress() { return imgProgress; } public void setImgProgress(ImageProgress imgProgress) { this.imgProgress = imgProgress; } } 

The following is a class of service from a server named HpcService that will return an HPCResponse object as a response. As you can see, the startAnalysing method accepts an HPCInfo object. HPCInfo is also described below.

 @Path( "/hpc" ) @Consumes( MediaType.APPLICATION_XML ) @Produces( MediaType.APPLICATION_XML ) public class HpcService{ public HPCInfo hpcInfo; public HPCResponse hpcResponse; @POST @Path( "/analyze" ) public HPCResponse startAnalysing(HPCInfo _hpcInfo){ System.out.println( "Started Analyzing..." ); hpcInfo = _hpcInfo; hpcInfo.getImagePath(); hpcResponse = new HPCResponse(); ImageProgress iProg = new ImageProgress(hpcInfo.getImagePath()); hpcResponse.setImgProgress(iProg); System.out.println("Returning response..."); return hpcResponse; } } 

The HPCInfo class is also available on both the client and server. HPCInfo Class:

  @XmlRootElement public class HPCInfo { private String imagePath = ""; public String getImagePath(){ return imagePath; } public void setImagePath( String imagePath ){ this.imagePath = imagePath; } } 

And finally, my client calls HPCService.

 public class TestClient { private static String webServiceURI = "http://localhost:8080/TestServer123"; public static void main(String[] args) { String input = "ABNKidney.scn"; ClientConfig clientConfig = new ClientConfig(); Client client = ClientBuilder.newClient(clientConfig); URI serviceURI = UriBuilder.fromUri(webServiceURI).build(); WebTarget webTarget = client.target(serviceURI); HPCInfo info = new HPCInfo(); info.setImagePath(input); webTarget = webTarget.path("test").path("hpc").path("analyze"); HPCResponse hResponse = webTarget.request().accept(MediaType.APPLICATION_XML).post(Entity.entity(info, MediaType.APPLICATION_XML), HPCResponse.class); } } 

This is a complete description of the error I am getting:

 javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:968) at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:795) at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91) at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683) 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:424) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:435) at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:338) at com.TestClient.main(TestClient.java:34) 
+13
java rest jersey glassfish
source share
2 answers

One way to debug such things is to create a simple ExceptionMapper to catch exceptions that are not displayed. When there is no mapmaker, an exception often pops up to the container level, which just gives us a general 500 server error (which helps most of the time).

 @Provider public class DebugExceptionMapper implements ExceptionMapper<Exception> { @Override public Response toResponse(Exception exception) { exception.printStackTrace(); return Response.serverError().entity(exception.getMessage()).build(); } } 

Then just register the cartographer. When you perform a simple test with your ImageProgress class, when an exception is thrown, a stack fingerprint is displayed and you can see the exception message

... ImageProgress has no default constructor with no arguments

So just add the standard value (constructor with no arguments) to the ImageProgress class. This is a requirement for JAXB models.

+20
source

Add this line of code to the class HPCResponse :

 @XmlAccessorType(XmlAccessType.FIELD) 
0
source

All Articles