Multiple response types with the same REST GET?

I want to create a REST service that can return JSON or XML. What request parameter is specified in the request for a request of a particular mime type? I know how to set it in a response, but there must be a way to request a specific one. I am currently doing this in the URL

restServlet / engine / 2WS2345

jsonServlet / engine / 2WS2345

This gets me json or xml. But I thought I read that there is a parameter in the request. I am using JAVA ...

+5
source share
2 answers

If you use a T-shirt, you can easily customize the method using the @Produces annotation. @Produces ({"application / xml", "application / json"})

, JAXB . . MIME- Accept, xml .

Ref http://jersey.java.net/nonav/documentation/1.6/user-guide.html

+4

Restlet - Accept URI ( Restlet TunnelService MetadataService). ( Restlet 2):

public class TestApplication extends Application {
    public static class TestResource extends ServerResource {
        @Get("txt")
        public Representation toText() {
            return new StringRepresentation("Hello!",
                MediaType.TEXT_PLAIN);
        }

        @Get("xml")
        public Representation toXml() {
            return new StringRepresentation("<test>Hello</test>",
                MediaType.APPLICATION_XML);
        }
    }

    @Override
    public synchronized Restlet createInboundRoot() {
        getTunnelService().setEnabled(true);
        getTunnelService().setExtensionsTunnel(true);
        Router router = new Router();
        router.attachDefault(TestResource.class);
        return router;
    }

    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);
        component.getDefaultHost().attachDefault(new TestApplication());
        component.start();
    }
}

Accept:

  • curl -H "Accept: text/plain" http://localhost:8182/test Hello!
  • curl -H "Accept: application/xml" http://localhost:8182/test <test>Hello</test>

( getTunnelService().setExtensionsTunnel(true)):

  • curl http://localhost:8182/test.txt Hello!
  • curl http://localhost:8182/test.xml <test>Hello</test>

-, MetadataService.

+5

All Articles