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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 如何使用 Spring 創建 RESTful Web 服務 > 原文: [https://javatutorial.net/how-to-create-restful-web-services-with-spring](https://javatutorial.net/how-to-create-restful-web-services-with-spring) 在處理 RESTful Web 服務時,我們需要使用`@RestController`注釋,該注釋基本上表示`@Controller`和`@ResponseBody`注釋。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) 當我們在方法中使用`@RequestMapping`時,我們可以添加一個名為產生的屬性,該屬性指定發送給用戶的輸出將為 JSON 格式。 ### 示例 **Employee.java** ```java public class Employee { private int id; private String firstName; private String lastName; public Employee(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } public setFirstName(String fName) { firstName = fName; } public setLastName(String lName) { lastName = lName; } public int getId() { return id; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } } ``` `EmployeeController.java` 控制器類將可用于處理 HTTP 請求,這是我們構建 RESTful Web 服務時的約定。 ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class EmployeeController { @RequestMapping("/employee") public Employee createEmployee(@RequestParam(value="id") int employeeId, @RequestParam(value="firstName") String fName, @RequestParam(value="lastName") String lName) { // return the new Employee object with that id, first name and last name return new Employee(employeeId, fName, lName); } } ``` 讓我們分解`Controller`類。 乍一看,它看起來很簡單,但是在幕后發生了很多事情。 讓我確切解釋一下。 感謝`@RequestMapping`,我們將路徑`/employee`映射到我們的`getEmployeeId`方法,如果您不熟悉`@RequestMapping`注釋,則當我們不指定方法請求時,默認情況下它將通過。 如果我們只想將它作為 GET 方法使用(在我們的示例中最好),則可以將上面的代碼更改為以下代碼: ```java import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class EmployeeController { @RequestMapping("/employee", method=RequestMethod.GET) public Employee createEmployee(@RequestParam(value="id") int employeeId, @RequestParam(value="firstName") String fName, @RequestParam(value="lastName") String lName) { //?return?the?new?Employee?object?with?that?id,?first?name?and?last?name? return new Employee(employeeId, fName, lName); } } ``` 因此現在我們將`@RequestMapping`注解更改為`@RequestMapping("/employee", method=RequestMethod.GET)`。正如我在本教程開始時所說的,我們還指定了我們希望結果為 JSON 格式。 因此我們可以將`@RequestMapping("/employee", method=RequestMethod.GET)`更改為`@RequestMapping("/employee", method=RequestMethod.GET, Produces="application/json")`。 我們將查詢參數`id`提取到`employeeId`,將`firstName`提取到`fName`,將`lastName`提取到`lName`,然后實例化一個**新的**`Employee`對象并首先傳遞該 ID。 我們作為查詢參數得到的名字和姓氏。 要檢索這些參數并使用此方法創建`Employee`對象,URL 如下所示: ``` http://localhost:8080/employee?id=x&firstName=xx&lastName=xxx ``` 再一次,感謝`@RequestParam`,我們得到了`x`,并將它們存儲在方法參數中。 ### 運行應用程序 要運行該應用程序,我們將使用`main()`方法,該方法如下所示: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `SpringApplication.run()`啟動應用程序并執行您的所有操作。 這是最簡單的方法,但是,如果愿意,可以將其設置為可執行的 JAR,也可以使用 Gradle 或 Maven 從命令行運行它。 你的選擇。 ### 響應 JSON 格式的響應主體如下所示: ```java { "id":x, firstName="xx", lastName="xxx" } ```
                  <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>

                              哎呀哎呀视频在线观看