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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Jersey + JSONP 示例 > 原文: [https://howtodoinjava.com/jersey/jax-rs-jersey-jsonp-example/](https://howtodoinjava.com/jersey/jax-rs-jersey-jsonp-example/) 本教程介紹了如何在 Jersey2.x 中使用 [**JSONP**](https://en.wikipedia.org/wiki/JSONP) JSON 供應器。 就像我們在 [Jersey MOXy 示例](//howtodoinjava.com/jersey/jax-rs-jersey-moxy-json-example/)中討論的一樣,JSONP 也可以自動發現。 ```java Table of Contents JSONP maven dependencies/changes REST API code Model bean changes Manually adding JsonProcessingFeature ``` ## JSONP Maven 依賴項/更改 JSONP 媒體模塊是 Jersey2.x 中的模塊之一,您無需明確注冊其功能,例如[`JsonProcessingFeature`](https://jersey.java.net/apidocs/2.0/jersey/org/glassfish/jersey/jsonp/JsonProcessingFeature.html)。 一旦 Jersey 檢測到添加了它的存在,它就會自動注冊。 因此,只需在`pom.xml`中添加 JSONP 依賴項就可以完成一半的工作。 ```java <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-processing</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 + "]"; } } ``` ## 手動添加`JsonProcessingFeature` 盡管`org.glassfish.jersey.jsonp.JsonProcessingFeature`是自動注冊的,但是如果您希望手動注冊,則可以按以下配置添加它。 ```java public class CustomApplication extends ResourceConfig { public CustomApplication() { register(JsonProcessingFeature.class); packages("com.howtodoinjava.jersey"); packages("org.glassfish.jersey.examples.jsonp"); register(LoggingFilter.class); property(JsonGenerator.PRETTY_PRINTING, true); } } ``` 并將此`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> ``` 在下面將您的問題和評論發送給我。 **祝您學習愉快!** **參考**: [https://jersey.java.net/documentation/latest/media.html#json.json-p](https://jersey.java.net/documentation/latest/media.html#json.json-p)
                  <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>

                              哎呀哎呀视频在线观看