- MOXy作為您的JAX-RS JSON提供程序–服務器端
- MOXy作為您的JAX-RS JSON提供程序–客戶端
URI
這篇文章將重點介紹我們在上一篇文章中聲明的服務中的以下URI。 以下呼叫將返回居住在“任何城鎮”的客戶列表。
http://localhost:8080/CustomerService/rest/customers/findCustomersByCity/Any%20Town
Java SE客戶端API
在第一個示例中,我們將使用標準的Java SE 6 API。 一些有趣的注意事項:
- MOXy可以直接封送(第35行)和解封(第28行)往返于JSON數組的集合,而無需包裝對象。
- MOXy沒有編譯時間依賴性(它是運行時依賴性)。
- eclipselink.media-type屬性用于在unmarshaller(第25行)和marshaller(第33行)上啟用JSON綁定。
- eclipselink.json.include-root屬性用于指示@XmlRootElement批注在JSON綁定中應被忽略(第26和34行)。
package example;import java.io.InputStream;
import java.net.*;
import java.util.List;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.example.Customer;public class JavaSEClient {private static final String MEDIA_TYPE = "application/json";public static void main(String[] args) throws Exception {String uri = "http://localhost:8080/CustomerService/rest/customers/findCustomersByCity/Any%20Town";URL url = new URL(uri);HttpURLConnection connection =(HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setRequestProperty("Accept", MEDIA_TYPE);JAXBContext jc = JAXBContext.newInstance(Customer.class);Unmarshaller unmarshaller = jc.createUnmarshaller();unmarshaller.setProperty("eclipselink.media-type", MEDIA_TYPE);unmarshaller.setProperty("eclipselink.json.include-root", false);InputStream xml = connection.getInputStream();List<Customer> customers = (List<Customer>) unmarshaller.unmarshal(new StreamSource(xml), Customer.class).getValue();connection.disconnect();Marshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.setProperty("eclipselink.media-type", MEDIA_TYPE);marshaller.setProperty("eclipselink.json.include-root", false);marshaller.marshal(customers, System.out);}}
輸出量
以下是運行Java SE客戶端的輸出。 對于那些可能使用JAXB( JSR-222 )實現和Jettison之類的東西來產生/使用JSON的人,以下是一些值得注意的有趣事項:
- MOXy將集合呈現為JSON數組。
- MOXy正確表示數字值而不帶引號(第26行)。
- MOXy用方括號正確地包圍了大小為1的集合(第28和32行)。
[ {"address" : {"city" : "Any Town","id" : 1,"street" : "1 A Street"},"firstName" : "Jane","id" : 1,"lastName" : "Doe","phoneNumbers" : [ {"id" : 2,"num" : "555-2222","type" : "HOME"}, {"id" : 1,"num" : "555-1111","type" : "WORK"} ]
}, {"address" : {"city" : "Any Town","id" : 10,"street" : "456 Another Road"},"firstName" : "Sue","id" : 10,"lastName" : "Jones","phoneNumbers" : [ {"id" : 10,"num" : "555-3333","type" : "WORK"} ]
} ]
澤西島客戶端API
JAX-RS 2.0( JSR-339 )正在致力于標準化客戶端API。 使用JAX-RS 1.0,許多實現都提供了自己的版本。 以下是使用Jersey提供的客戶端API的示例。 注意我們如何利用服務器端使用的完全相同的MessageBodyReader / Writer (第14行,將MOXy稱為JAX-RS JSON提供程序–服務器端 )。 我還指定了LoggingFilter (第17行),因此我們可以仔細查看該消息。
package example;import java.util.List;
import org.example.Customer;
import org.example.MOXyJSONProvider;
import com.sun.jersey.api.client.*;
import com.sun.jersey.api.client.config.*;
import com.sun.jersey.api.client.filter.LoggingFilter;public class JerseyClient {public static void main(String[] args) {ClientConfig cc = new DefaultClientConfig();cc.getClasses().add(MOXyJSONProvider.class);Client client = Client.create(cc);client.addFilter(new LoggingFilter());WebResource resource = client.resource("http://localhost:8080/CustomerService/rest/customers");List<Customer> customers = resource.path("/findCustomersByCity/Any%20Town").accept("application/json").get(new GenericType<List<Customer>>(){});for(Customer customer : customers) {System.out.println(customer.getFirstName());}}}
輸出量
以下是運行Jersey客戶端的輸出。
14-Mar-2012 4:08:12 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > GET http://localhost:8080/CustomerService/rest/customers/findCustomersByCity/Any%20Town
1 > Accept: application/json
1 > 14-Mar-2012 4:08:12 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client in-bound response
1 < 200
1 < Transfer-Encoding: chunked
1 < Date: Wed, 14 Mar 2012 20:08:12 GMT
1 < Content-Type: application/json
1 < X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.1 Java/Oracle Corporation/1.7)
1 < Server: GlassFish Server Open Source Edition 3.1.1
1 <
[{"address" : {"city" : "Any Town", "id" : 1, "street" : "1 A Street"}, "firstName" : "Jane", "id" : 1, "lastName" : "Doe", "phoneNumbers" : [{"id" : 1, "num" : "555-1111", "type" : "WORK"}, {"id" : 2, "num" : "555-2222", "type" : "HOME"}]}, {"address" : {"city" : "Any Town", "id" : 10, "street" : "456 Another Road"}, "firstName" : "Sue", "id" : 10, "lastName" : "Jones", "phoneNumbers" : [{"id" : 10, "num" : "555-3333", "type" : "WORK"}]}]Doe, Jane
Jones, Sue
進一步閱讀
如果您喜歡這篇文章,那么您可能也會對以下內容感興趣:
- RESTful服務
- MOXy作為您的JAX-RS JSON提供程序–服務器端
- 創建一個RESTful服務
- 第1部分–數據庫
- 第2部分–將數據庫映射到JPA實體
- 第3部分–將JPA實體映射到XML(使用JAXB)
- 第4部分– RESTful服務
- 第五部分–客戶
- JAX-RS服務中的MOXy的XML元數據
- JSON綁定
- 使用EclipseLink MOXy進行JSON綁定– Twitter示例
- 綁定到JSON和XML –地理編碼示例
參考: MOXy作為您的JAX-RS JSON提供程序–來自Java XML和JSON綁定博客的JCG合作伙伴 Blaise Doughan的客戶端 。
翻譯自: https://www.javacodegeeks.com/2012/04/moxy-as-your-jax-rs-json-provider_18.html