简体   繁体   中英

Returning response using Jersey REStful web services?

I am using JAX-RS web services (Jersey). I have a pojo User.java . This pojo is not generated from XSD. This pojo is handwritten. Can i return such a pojo using REStful web service method ? Also, is it mandatory to write XSD while using Restful WEbservices?

@GET
@Produces ("application/xml")
public List<User> getUsersAll() {
    List<User> als=null;
    try {
        als= UserService.getInstance().getUserAll();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return als;
}

Is above code possible without jaxb generated object User,java and only with handwritten User.java? Also, is it good practice to write XSD always? Thanks!

Can I return such a pojo using RESTful web service method?

---> Yes, you can.

Also, is it mandatory to write XSD while using RESTful web services?

---> No, it's not mandatory have an XSD.

Normally it's good to generate or write an XSD
even if you wrote the Java class by hand.
That's because people/clients using your WS will
probably want some "model" from you which basically
means they want an XSD.

For Jersey you can do this:

<servlet>
    <servlet-name>test-rest-service</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.container.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.test.rest.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Tomcat will scan this package and look for resources
there which are mapped to URL paths via annotations. The
WADL will be auto-generated at runtime. But this WADL will
not have XSDs which the client can use to validate the
data it sends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM