JAX-RS - JSON without root node

I have a calm web service and the answer is:

{ "cities": [{ "id": "1", "name": "City 01", "state": "A1" }, { "id": "2", "name": "City 02", "state": "A1" }] } 

But I want this:

 { [{ "id": "1", "name": "City 01", "state": "A1" }, { "id": "2", "name": "City 02", "state": "A1" }] } 

How can I configure JAX-RS to create JSON without root node using only the JAX-RS function and not the specifics of the implementation? My code must be portable on any application server.

+6
java json jax-rs
source share
3 answers

I had the same issue with Glassfish v3. I found that this behavior is dependent on the JAX-RS implementation, and switching to the Codehaus Jackson JAX-RS implementation has solved the problem for me.

If you also use Glassfish, you can solve the problem by adding org.codehaus.jackson.jaxrs to your war, as well as to the WEB-INF/web.xml as follows:

 <!-- REST --> <servlet> <servlet-name>RESTful Services</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name> <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value> </init-param> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>you.service.packages;org.codehaus.jackson.jaxrs</param-value> <!-- NOTE: The last element above, org.codehaus.jackson.jaxrs, replaces the default JAX-RS processor with the Codehaus Jackson JAX-RS implementation. The default JAX-RS processor returns top-level arrays encapsulated as child elements of a single JSON object, whereas the Jackson JAX-RS implementation return an array. --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>RESTful Services</servlet-name> <url-pattern>/your/rest/path/*</url-pattern> </servlet-mapping> 

Alternatively, you can simply intercept the response in the client:

 function consumesCity(json) { ... } 

Replace

 ... consumesCity(json) ... 

from

 function preprocess(json) { return json.city; } ... consumesCity(preprocess(json)) ... 
+4
source share
Good question. I had a requirement like this. I had to access the generated original answer and do some manipulation. I accomplished this by registering a response filter, and then adapt a custom reponsewriter. See the link below for more details.
 http://www.mentby.com/paul-sandoz/access-to-raw-xml-in-jersey.html 

In your response filter, you can cut the class name from the generated json or, rather, return the response of the String string and use your own json serialization mechanism, for example Google-gson.

Let me know if this solution works.

+1
source share

Kim Burgard's answer on works also for Spring WS Jersey. I had the same issue using Glassfish 3.0, and I decided to add the option shown below.

Web.xml example

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <servlet> <servlet-name>Jersey Spring Web Application</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>org.codehaus.jackson.jaxrs</param-value> <!-- NOTE: The last element above, org.codehaus.jackson.jaxrs, replaces the default JAX-RS processor with the Codehaus Jackson JAX-RS implementation. The default JAX-RS processor returns top-level arrays encapsulated as child elements of a single JSON object, whereas the Jackson JAX-RS implementation return an array.--> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey Spring Web Application</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app> 

Example applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- Scan for both Jersey Rest Annotations and persistence classes --> <context:component-scan base-package="your.service.packages"/> </beans> 
0
source share

All Articles