<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Jersey + MOXy JSON 示例 > 原文: [https://howtodoinjava.com/jersey/jax-rs-jersey-moxy-json-example/](https://howtodoinjava.com/jersey/jax-rs-jersey-moxy-json-example/) 本教程說明**如何在 Jersey2.x** 中使用 MOXy JSON 功能。 MOXy 是 Jersey2.x 中的**默認 JSON 綁定供應器**。 盡管由于性能原因,我個人還是更喜歡 [**Jackson**](http://jersey.576304.n2.nabble.com/Jackson-vs-MOXy-td7581625.html),而不是 MOXy。 ```java Table of Contents MOXy maven dependencies/changes REST API code Model bean changes Manually adding MoxyJsonFeature Customize behavior using MoxyJsonConfig Demo ``` ## MOXy Maven 依賴項/更改 MOXy 媒體模塊是 Jersey2.x 中的模塊之一,您無需在其中明確注冊其功能,例如`MoxyJsonFeature`。 一旦 Jersey 檢測到添加了它的存在,它就會自動注冊。 因此,只需在`pom.xml`中添加 MOXy 依賴項就可以完成一半的工作。 ```java <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>2.19</version> </dependency> ``` ## REST API 代碼 在編寫 API 的服務端,您需要使用`@Produces(MediaType.APPLICATION_JSON)`注解啟用 JSON 媒體類型。 #### `JerseyService.java` ```java @Path("/employees") public class JerseyService { @GET @Produces(MediaType.APPLICATION_JSON) public Employees getAllEmployees() { Employees list = new Employees(); list.setEmployeeList(new ArrayList<Employee>()); list.getEmployeeList().add(new Employee(1, "Lokesh Gupta")); list.getEmployeeList().add(new Employee(2, "Alex Kolenchiskey")); list.getEmployeeList().add(new Employee(3, "David Kameron")); return list; } } ``` ## 模型 bean 更改 在模型 bean 方面,您無需放置任何注解或任何配置。 默認情況下它將起作用。 您甚至也不需要添加任何根注解。 #### `Employees.java` ```java public class Employees { private List<Employee> employeeList; public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } } ``` #### `Employee.java` ```java public class Employee { private Integer id; private String name; public Employee() { } public Employee(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + "]"; } } ``` ## 手動添加`MoxyJsonFeature` 盡管`MoxyJsonFeature`是自動注冊的,但是如果您希望手動注冊,則可以按以下配置添加它。 ```java public class CustomApplication extends Application { //Add Service APIs @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<Class<?>>(); //register REST modules resources.add(JerseyService.class); //Manually adding MOXyJSONFeature resources.add(org.glassfish.jersey.moxy.json.MoxyJsonFeature.class); //Configure Moxy behavior resources.add(JsonMoxyConfigurationContextResolver.class); return resources; } } ``` 并將此`Application`類添加到`web.xml`文件中。 ```java <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.howtodoinjava.jersey.CustomApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> ``` ## 使用`MoxyJsonConfig`自定義行為 MOXy 在 JAXB 之上提供了某些功能,您可以通過提供`MoxyJsonConfig`實現來啟用/禁用這些功能。 ```java //Register ContextResolver<MoxyJsonConfig> to override //default behavior of marshaling/un-marshaling attributes @Provider public class JsonMoxyConfigurationContextResolver implements ContextResolver<MoxyJsonConfig> { private final MoxyJsonConfig config; public JsonMoxyConfigurationContextResolver() { final Map<String, String> namespacePrefixMapper = new HashMap<String, String>(); namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); config = new MoxyJsonConfig() .setNamespacePrefixMapper(namespacePrefixMapper) .setNamespaceSeparator(':') .setAttributePrefix("") .setValueWrapper("value") .property(JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME, true) .setFormattedOutput(true) .setIncludeRoot(true) .setMarshalEmptyCollections(true); } @Override public MoxyJsonConfig getContext(Class<?> objectType) { return config; } } ``` `JsonMoxyConfigurationContextResolver`已被添加到上面的`CustomApplication`類中的行號 16。 ## 演示 只需將您的應用部署在任何服務器上,然后單擊 URL:`http://localhost:8080/JerseyDemos/rest/employees` 這將產生以下輸出。 ```java { "employeeList": [ { "id": 1, "name": "Lokesh Gupta" }, { "id": 2, "name": "Alex Kolenchiskey" }, { "id": 3, "name": "David Kameron" } ] } ``` 以下是演示應用中上述文件的層次結構。 ![Jersey2.x MOXy JSON Demo Application Structure](https://img.kancloud.cn/91/bc/91bc958a825f174a70997e575e2d0cce_544x469.png) Jersey2.x MOXy JSON 示例應用結構 在下面將您的問題和評論發送給我。 **祝您學習愉快!** **參考**: [https://jersey.java.net/documentation/latest/media.html#json.moxy](https://jersey.java.net/documentation/latest/media.html#json.moxy)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看