<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring REST 控制器示例 > 原文: [https://howtodoinjava.com/spring-restful/how-to-write-restful-webservices-using-spring-3-mvc/](https://howtodoinjava.com/spring-restful/how-to-write-restful-webservices-using-spring-3-mvc/) 學習創建 **Spring REST 控制器**,該控制器可以處理任何 [Spring MVC](https://howtodoinjava.com/spring-mvc-tutorial/) 應用程序中的 REST API 調用。 它邀請添加`@Controller`和`@RequestMapping`注解。 為了編寫此應用程序,我正在修改[ Spring MVC 示例](https://howtodoinjava.com/jstl/spring-3-mvc-hello-world-application-with-maven-and-jstl/)中編寫的源代碼。 因此,如果需要,您可以從給定的鏈接下載源代碼。 ## 1\. 更新 Maven 依賴項 更新`pom.xml`以添加對 [JAXB](https://howtodoinjava.com/jaxb/read-xml-to-java-object/) 和 Jackson 的支持(適用于 **xml** 和 **json** 格式)。 ```java <dependency> <groupid>org.codehaus.jackson</groupid> <artifactid>jackson-mapper-asl</artifactid> <version>${jackson-mapper-asl.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupid>javax.xml.bind</groupid> <artifactid>jaxb-api</artifactid> <version>${jaxb-api.version}</version> <scope>runtime</scope> </dependency> ``` ## 2\. 添加`ContentNegotiatingViewResolver` 更新視圖解析器的 bean 配置文件,并添加`ContentNegotiatingViewResolver`。 ```java <mvc:annotation-driven /> <context:component-scan base-package="com.howtodoinjava.web" /> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="html" value="text/html"></entry> <entry key="json" value="application/json"></entry> <entry key="xml" value="application/xml"></entry> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </list> </property> </bean> ``` ## 3\. 在模型類中添加 JAXB 注解 我正在編寫 2 個類,即`Users.java`和`User.java`。 這些類將具有 **JAXB 注解**,marshaller 將使用它們將其轉換為適當的 xml 或 json 格式。 它們僅作為示例,您可以編寫自己的類。 `Users.java` ```java package com.howtodoinjava.model; import java.util.Collection; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="users") @XmlAccessorType(XmlAccessType.NONE) public class Users { @XmlElement(name="user") private Collection<User> users; public Collection<User> getUsers() { return users; } public void setUsers(Collection<User> users) { this.users = users; } } ``` `User.java` ```java package com.howtodoinjava.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="user") @XmlAccessorType(XmlAccessType.NONE) public class User { @XmlElement(name="first-name") private String firstName; @XmlElement(name="last-name") private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } ``` ## 4\. 創建 REST 控制器 `DemoController.java`具有 REST 專用注解,用于請求參數映射中的路徑映射。 另外,我們將為請求和響應指定頭屬性。 `DemoController.java` ```java @Controller @RequestMapping("/users") public class DemoController { @RequestMapping(method = RequestMethod.GET, value="/{id}", headers="Accept=*/*") public @ResponseBody User getUserById(@PathVariable String id) { User user = new User(); user.setFirstName("john"); user.setLastName("adward"); return user; } @RequestMapping(method = RequestMethod.GET, headers="Accept=*/*") public @ResponseBody Users getAllUsers() { User user1 = new User(); user1.setFirstName("john"); user1.setLastName("adward"); User user2 = new User(); user2.setFirstName("tom"); user2.setLastName("hanks"); Users users = new Users(); users.setUsers(new ArrayList<User>()); users.getUsers().add(user1); users.getUsers().add(user2); return users; } } ``` ## 5\. Spring REST 示例 現在,讓我們在 tomcat 上重新部署應用程序,并在任何 REST 客戶端上訪問 URL。 我正在使用[`RESTClient`](https://addons.mozilla.org/en-US/firefox/addon/restclient/)。 這是一個用于測試 RESTful Web 服務的 firefox 插件。 * **URL: `http://localhost:8080/firstSpringApplication/users`** ![http://localhost:8080/firstSpringApplication/users](https://img.kancloud.cn/65/65/6565188f85e8b12cef822366780b1cd3_937x548.jpg) * **URL: `http://localhost:8080/firstSpringApplication/users/123`** ![http://localhost:8080/firstSpringApplication/users/123](https://img.kancloud.cn/bc/9b/bc9b14ed74e6da86720ba899dafa3be8_942x450.jpg) [下載源碼](https://drive.google.com/file/d/0B7yo2HclmjI4aGMxOF9aVTNkR1E/view?usp=drive_web) 如果確實有幫助,請給我評論,或者您有任何疑問。 學習快樂!
                  <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>

                              哎呀哎呀视频在线观看