Glassfish JAX-RS JSON Mapping Simple Example 500 Server Internal Error No Logs

I created a simple REST service with two resources. The first resource works fine and just returns MediaType.TEXT_PLAIN.

For the second resource, I wanted to try mapping POJOs to Java and follow this example:

https://github.com/jersey/jersey/tree/2.3.1/examples/json-moxy/src/main/java/org/glassfish/jersey/examples/jsonmoxy

with my testbean is defined as:

@XmlRootElement public class Company { public String name; public String symbol; public String country; public Company(String name, String symbol, String country) { this.name = name; this.symbol = symbol; this.country = country; } public String getName() { return name; } public String getSymbol() { return symbol; } public String getCountry() { return country; } } 

The resource is also trivial:

 @Path("company/{name}") public class CompanyResource { private Map<String, Company> companies; public CompanyResource() { companies = new LinkedHashMap<String, Company>(); companies.put("Apple", new Company("Apple Inc.", "AAPL", "USA")); companies.put("Microsoft", new Company("Microsoft Corp.", "MSFT", "USA")); companies.put("Honda", new Company("Honda Motor Co Ltd", "HMC", "Japan")); companies.put("Random", new Company("Random Inc.", "RND", "Undefined")); } @GET @Produces(MediaType.APPLICATION_JSON) public Company getCompany(@PathParam("name") String name) { Company cmp = companies.get(name); if (cmp == null) { return companies.get("Random"); } return cmp; } } 

I debugged the request and returned to the return statement without any problems. From here, however, I think that a JAXBException is being thrown, but I cannot see the details, and nothing appears anywhere in any log. All that happens is that the browser displays the message "500 internal server error."

In the monitoring configuration, I desperately set everything to the HIGH level. Still nothing appears.

The only similar questions I found were jax-rs 2.0 and Glassfish 4, unable to @consume JSON in Pojo and JAX RS Jersey + JSON → HTTP 500 Internal Server Error , but they were not completely related.

For the client, I just use Google Chrome using the "Advanced REST Client" application.

Any help would be greatly appreciated.

+7
json rest jax-rs glassfish jaxb
source share
3 answers

You will need to create a no-arg constructor in your Company class. If you want to restrict access to the constructor, you can make it private.

 private Company() { } 
+11
source share

Try putting the "name" parameter in a method not in the class as follows:

 @Path("company") public class CompanyResource { private Map<String, Company> companies; public CompanyResource() { companies = new LinkedHashMap<String, Company>(); companies.put("Apple", new Company("Apple Inc.", "AAPL", "USA")); companies.put("Microsoft", new Company("Microsoft Corp.", "MSFT", "USA")); companies.put("Honda", new Company("Honda Motor Co Ltd", "HMC", "Japan")); companies.put("Random", new Company("Random Inc.", "RND", "Undefined")); } @GET @PathParam("{name}") @Produces(MediaType.APPLICATION_JSON) public Company getCompany(@PathParam("name") String name) { Company cmp = companies.get(name); if (cmp == null) { return companies.get("Random"); } return cmp; } } 

if you want to use it as you showed, you need to add a constructor named "name" as a field:

 public CompanyResource(@PathParam("name") String name) { this.name = name; } 
0
source share

One of them, unfortunately not documented, is a problem that can happen:

If you declare a transient field , do not comment on it with @XmlTransient . Transient fields are effectively xml-transitive, and you no longer need annotation.

This happened when I violated this rule in the desk field:

 2016-11-30T14:09:33.731-0500|Severe: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: IllegalAnnotationException Transient field "desk" cannot have any JAXB annotations. this problem is related to the following location: at @javax.xml.bind.annotation.XmlTransient() at my.package.Desk 
0
source share

All Articles