Glassfish JAX-RS JSON Mapping Simple Example Inter

2020-02-07 01:41发布

问题:

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

For the second resource I wanted to try mapping POJO to Java and followed 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 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 trivial as well:

@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 have debugged a request and arrive at the return statement without problems. From here however, I think a JAXBException is thrown but I am unable to view the details, and nothing appears in any log anywhere. All that happens is that the browser display an "internal server error 500" message.

Under monitoring configuration I have in desperation set everything to level HIGH. Still nothing appears anywhere.

The only similar questions I found were jax-rs 2.0 and Glassfish 4 unable to @consume JSON into Pojo and JAX RS Jersey + JSON --> HTTP 500 Internal Server Error but they didn't seem entirely related.

For client I am simply using Google Chrome with the "Advanced REST Client" app.

Any help would be much appreciated.

回答1:

You will need to provide a no-arg constructor on your Company class. If you want to limit who can access constructor you can make it private.

private Company() {
}


回答2:

Try to put the param "name" in the method not in the class like this :

@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 the way you showed, you have to add a constructor with the "name" as a field :

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


回答3:

One — unfortunately under-documented — issue that may happen:

If you declare a field transient don't annotate it with @XmlTransient. transient fields are effectively xml-transient and you don't need the annotation anymore.

This happened when I violated this rule, on a field desk:

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