<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國際加速解決方案。 廣告
                # Spring REST XML 響應示例 > 原文: [https://howtodoinjava.com/spring-restful/spring-rest-hello-world-xml-example/](https://howtodoinjava.com/spring-restful/spring-rest-hello-world-xml-example/) 在這個 Spring REST XML 示例中,我正在使用 [**Spring REST**](https://howtodoinjava.com/category/spring/spring-restful/) 功能編寫 [**REST API**](https://howtodoinjava.com/restful-web-service/) 的世界示例。 在此示例中,我將創建兩個 API,這些 API 將返回資源的 XML 表示形式。 [**下載源碼**](https://drive.google.com/file/d/0B7yo2HclmjI4b2tlR2N5enVvVHM/view?usp=sharing) ## 1\. Maven 依賴 讓我們從運行時依賴關系開始,您需要編寫這些 REST API。 實際上,您只需要 Spring MVC 支持。 `pom.xml` ```java <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.howtodoinjava.demo</groupId> <artifactId>springrestexample</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springrestexample Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Spring MVC support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>springrestexample</finalName> </build> </project> ``` > 注意:如果您還計劃包括 JSON 支持,那么您要做的就是將 Jackson 庫包含到類路徑中,并且相同的 API 也將適用于 jackson。 ```java <!-- Jackson JSON Processor --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.1</version> </dependency> ``` ## 2\. Spring MVC 配置 為了創建 API,您需要像在 [**Spring MVC**](https://howtodoinjava.com/category/frameworks/java-spring-tutorials/spring-mvc/) 中一樣配置應用程序。 `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>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` `spring-servlet.xml` ```java <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.howtodoinjava.demo" /> <mvc:annotation-driven /> </beans> ``` ## 3\. JAXB 注解的模型對象 您將需要用 jaxb 注解來注解模型對象,以便 [**JAXB**](https://howtodoinjava.com/category/frameworks/jaxb/) 可以將 Java 對象編組為 XML 表示形式,以發送給該 API 的客戶端。 `EmployeeVO.java` ```java package com.howtodoinjava.demo.model; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name = "employee") @XmlAccessorType(XmlAccessType.NONE) public class EmployeeVO implements Serializable { private static final long serialVersionUID = 1L; @XmlAttribute private Integer id; @XmlElement private String firstName; @XmlElement private String lastName; @XmlElement private String email; public EmployeeVO(Integer id, String firstName, String lastName, String email) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.email = email; } public EmployeeVO(){ } //Setters and Getters @Override public String toString() { return "EmployeeVO [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; } } ``` `EmployeeListVO.java` ```java package com.howtodoinjava.demo.model; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name="employees") public class EmployeeListVO implements Serializable { private static final long serialVersionUID = 1L; private List<EmployeeVO> employees = new ArrayList<EmployeeVO>(); public List<EmployeeVO> getEmployees() { return employees; } public void setEmployees(List<EmployeeVO> employees) { this.employees = employees; } } ``` ## 4\. REST 控制器 這是主類,它將決定哪個 API 將以哪種方式運行。 `EmployeeRESTController.java` ```java package com.howtodoinjava.demo.controller; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.howtodoinjava.demo.model.EmployeeListVO; import com.howtodoinjava.demo.model.EmployeeVO; @RestController public class EmployeeRESTController { @RequestMapping(value = "/employees") public EmployeeListVO getAllEmployees() { EmployeeListVO employees = new EmployeeListVO(); EmployeeVO empOne = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com"); EmployeeVO empTwo = new EmployeeVO(2,"Amit","Singhal","asinghal@yahoo.com"); EmployeeVO empThree = new EmployeeVO(3,"Kirti","Mishra","kmishra@gmail.com"); employees.getEmployees().add(empOne); employees.getEmployees().add(empTwo); employees.getEmployees().add(empThree); return employees; } @RequestMapping(value = "/employees/{id}") public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id") int id) { if (id <= 3) { EmployeeVO employee = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com"); return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK); } return new ResponseEntity(HttpStatus.NOT_FOUND); } } ``` 讓我們記下一些重要的事情。 * 我們使用了[`@RestController`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html)注解。直到 Spring 3,我們可以使用[`@Controller`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Controller.html)注解,這種情況下使用[`@ResponseBody`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html)注解也很重要。 ```java @Controller public class EmployeeRESTController { @RequestMapping(value = "/employees") public @ResponseBody EmployeeListVO getAllEmployees() { //API code } } ``` > Spring 4 引入了`@RestController`,它是`@Controller + @ResponseBody`的組合。 因此,在使用`@RestController`時,無需使用`@ResponseBody`。 它是可選的。 * 在這里,我們依靠 Spring MVC `HttpMessageConverter`將對象轉換為用戶請求的 xml 表示形式。`@ResponseBody`注解(通過`@RestController`包含)告訴 Spring MVC 應該將方法的結果用作響應的主體。 正如我們想要的 XML 一樣,這種封送處理由 Spring 提供的`Jaxb2RootElementHttpMessageConverter`完成,如果在類路徑中找到 JAXB 庫,則會在 Spring 上下文中自動注冊。 由于我正在使用 JRE 7 運行此應用程序,并且它具有內置的 JAXB,因此不需要通過 maven 添加外部依賴項。 * 由于有了`@ResponseBody`注解,我們不再需要視圖名稱,而只需返回**員工**對象。 * 您可以將它們包裝在`ResponseEntity`中,而不是直接返回java對象。 `ResponseEntity`是 Spring MVC 中的一個類,與 HTTP 狀態代碼一起用作結果主體的對象的包裝。 這樣可以更好地控制您在各種用例中返回給客戶端的內容。 例如如果找不到給定員工 ID 的員工,則返回 404 錯誤。 ## 5\. 項目結構 ![Spring REST XML Example - Project Structure](https://img.kancloud.cn/08/98/08989444ad6ea375ee338d506f14f6ea_433x435.jpg) Spring REST XML 示例 – 項目結構 ## 測試 API 讓我們測試以上 REST API。 **1)點擊網址:`http://localhost:8080/springrestexample/employees`** 您也可以傳遞接受標頭“`application/xml`”。 ![Spring REST XML Example - REST API for get all employees](https://img.kancloud.cn/af/a6/afa681126bdc79cdce3fe283619e5759_753x476.jpg) Spring REST XML 示例 – 用于獲取所有員工的 REST API **2)點擊網址:`http://localhost:8080/springrestexample/employees/1`** ![Spring REST XML Example - REST API for get employee by id](https://img.kancloud.cn/52/a1/52a17138625daee1ee4d780d47c28096_477x253.jpg) Spring REST XML 示例 – 根據 ID 獲取員工的 REST API **3)點擊網址:`http://localhost:8080/springrestexample/employees/123`** ```java Status Code: 404 Not Found Content-Length: 0 Date: Fri, 18 Feb 2015 07:01:17 GMT Server: Apache-Coyote/1.1 ``` 這就是使用 spring mvc 的 REST API 的 **hello world** 簡單應用程序的全部內容。 [**下載源碼**](https://drive.google.com/file/d/0B7yo2HclmjI4b2tlR2N5enVvVHM/view?usp=sharing) 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看