2011-01-29 27 views
6

Tôi có một dịch vụ web yên tĩnh, và phản ứng là:JAX-RS - JSON mà không cần nút gốc

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

Nhưng tôi muốn điều này:

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

Làm thế nào tôi có thể cấu hình JAX-RS để tạo JSON mà không có nút gốc chỉ sử dụng tính năng JAX-RS và không phải là tính năng thực hiện cụ thể? Mã của tôi cần phải được di chuyển trên bất kỳ máy chủ ứng dụng nào.

+0

thế nào là mô hình của bạn (City) lớp JAXB chú thích? đây là những gì sẽ kiểm soát việc nối tiếp/deserialization đến/từ XML và JSON. –

+0

Lớp học của tôi dưới đây: @XmlRootElement (tên = "thành phố") lớp công khai CityDTO triển khai Serializable { } –

Trả lời

4

Tôi gặp vấn đề tương tự với Glassfish v3. Tôi thấy hành vi này phụ thuộc vào việc thực hiện JAX-RS và chuyển sang thực thi Codehaus 'Jackson JAX-RS đã giải quyết được vấn đề cho tôi.

Nếu bạn đang sử dụng Glassfish là tốt, sau đó bạn có thể giải quyết vấn đề bằng cách thêm org.codehaus.jackson.jaxrs đến chiến tranh của bạn cũng như đối với cấu hình WEB-INF/web.xml như sau:

<!-- 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> 

Ngoài ra, bạn có thể có thể đơn giản ngăn chặn các phản ứng trong ứng dụng:

function consumesCity(json) { 
    ... 
} 

Thay

... consumesCity(json) ... 

với

function preprocess(json) { 
    return json.city; 
} 

... consumesCity(preprocess(json)) ... 
+1

Cảm ơn bạn, Kim. Nhưng tôi vẫn sẽ tìm một giải pháp di động. –

+3

Tôi có ấn tượng về hành vi cụ thể này là nhà cung cấp cụ thể, tức là đối tượng JSON cấp cao nhất cho loại trả về thu thập sẽ là một mảng hoặc một đối tượng đơn lẻ có trường chứa các thành viên thu thập. Tôi hơi thất vọng khi thấy việc thực hiện tham chiếu của Glassfish trả về phong cách thứ hai cho các bộ sưu tập. Tôi nghĩ việc trả về một mảng JSON trực quan hơn nhiều. Vui lòng đăng cập nhật nếu bạn tìm thấy giải pháp tốt hơn. –

+1

Tôi đồng ý với Kim.Mặc dù các cấu trúc thực sự khác nhau - chủ yếu là do lý do lịch sử và đặc biệt vì có libs xử lý JSON thông qua API XML, đôi khi buộc sử dụng trình bao bọc - không thực sự là cách tiêu chuẩn hơn trừ khi bạn muốn sử dụng tùy chỉnh Triển khai MessageBodyWriter/Reader. – StaxMan

1

Câu hỏi hay. Tôi đã có một yêu cầu tương tự như thế này. Tôi đã có quyền truy cập vào phản ứng thô được tạo ra và thực hiện một số thao tác. Tôi đã đạt được điều đó bằng cách đăng ký bộ lọc phản hồi và sau đó điều chỉnh một người viết phản hồi tùy chỉnh. Xem liên kết bên dưới để biết thêm chi tiết.

http://www.mentby.com/paul-sandoz/access-to-raw-xml-in-jersey.html 

Trong bộ lọc phản hồi, bạn có thể cắt tên lớp từ json được tạo ra hoặc tốt hơn, trả về Chuỗi phản hồi và sử dụng cơ chế tuần tự json tùy chỉnh như Google-gson.

Hãy cho tôi biết nếu giải pháp này hoạt động.

0

Trả lời từ Kim Burgaard ở trên cũng hoạt động cho Jersey Spring WS. Tôi đã có cùng một vấn đề bằng cách sử dụng Glassfish 3.0 và giải quyết nó thêm thông số được hiển thị bên dưới.

Ví dụ web.xml

<?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> 

Ví dụ 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> 
Các vấn đề liên quan