简体   繁体   中英

How to pass pojo object as parameter to rest web service from prototypejs client

How to pass pojo object as parameter to rest web service from prototypejs client.

Assume i have web service like this.

@Path("/postItem")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Item postItem(Item item) 
{
  return new item;
}

From client side using prototypejs library how to pass pojo object as parameter to rest web service.

If parameter is of type string or integer i would have passed it as query param but it is pojo object in case.

I am about the syntax creation of the pojo object and then passing it to rest web service from prototypejs.

Which jax-rs implementation do you use? for Jersey, you can post json to the server by specify the jersey servlet:

<servlet>
    <servlet-name>jersey-servlet</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>your package</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

then in your client code, you can directly post a json to the server, which will be deserialized to an Item instance

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