Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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() {
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...