REST. Jersey. How to programmatically choose what type to return: JSON or XML?

后端 未结 3 1626
深忆病人
深忆病人 2021-02-04 11:53

I have 2 questions:

1. Can I create one class, annotate it with JAXB annotations(for XML support) and declare in web.xml

3条回答
  •  [愿得一人]
    2021-02-04 12:36

    Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.


    Can I create one class, annotate it with JAXB annotations(for XML support) and declare in web.xml for JSON (Jackson library) support?

    You can always use an Application class to specify a MessageBodyReader/MessageBodyWriter for the JSON binding. I believe Jackson provides an implementation in its jar. Below is an example of an Application class that specifies MOXy as the JSON provider:

    package org.example;
    
    import java.util.*;
    import javax.ws.rs.core.Application;
    import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
    
    public class CustomerApplication  extends Application {
    
        @Override
        public Set> getClasses() {
            HashSet> set = new HashSet>(2);
            set.add(MOXyJsonProvider.class);
            set.add(CustomerService.class);
            return set;
        }
    
    }
    

    Or I need to create separately two classes for JSON and XML?

    EclipseLink JAXB (MOXy) offers native XML binding and is designed to enable you to use the same object model for both JSON and XML. You can integrate it into your JAX-RS application using the MOXyJsonProvider class:

    • http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html

    How I can programmatically choose what type to return (JSON or XML)?

    Server Side

    You can specify that your service offers both XML and JSON messages using the @Produces annotation.

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("{id}")
    public Customer read(@PathParam("id") long id) {
        return entityManager.find(Customer.class, id);
    }
    

    For More Information

    • http://blog.bdoughan.com/2012/03/moxy-as-your-jax-rs-json-provider.html

    Client Side

    You can use the MediaType to indicate the type of message. Below is an example using Jersey client APIs. Note how the URL is the same, just the requested media type is different.

    Client client = Client.create();
    WebResource resource = client.resource("http://localhost:8080/CustomerService/rest/customers");
    
    // Get XML response as a Customer
    Customer customer = resource.path("1")
        .accept(MediaType.APPLICATION_XML)
            .get(Customer.class);
    System.out.println(customer.getLastName() + ", "+ customer.getFirstName());
    
    // Get JSON response as a Customer
    Customer customer = resource.path("1")
        .accept(MediaType.APPLICATION_JSON)
            .get(Customer.class);
    System.out.println(customer.getLastName() + ", "+ customer.getFirstName());
    

    For More Information

    • http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-55.html

提交回复
热议问题