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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring `RestTemplate` – Spring REST 客戶端示例 > 原文: [https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/](https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/) 在學習為[ XML 表示形式](https://howtodoinjava.com/spring/spring-restful/spring-rest-hello-world-xml-example/)和[ JSON 表示形式](https://howtodoinjava.com/spring/spring-restful/spring-rest-hello-world-json-example/)構建 **Spring REST API** 之后,讓我們構建 **Spring REST 客戶端**以使用我們編寫的 API 在鏈接的示例中。 在 Spring 應用程序內部訪問第三方 REST 服務的過程涉及 Spring [`RestTemplate`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html)類的使用。 `RestTemplate`類的設計原理與許多其他 Spring `*Template`類(例如`JdbcTemplate`和`JmsTemplate`)相同,為執行復雜任務提供了具有默認行為的簡化方法。 鑒于`RestTemplate`類是為調用 REST 服務設計的,因此它的主要方法與 REST 的基礎緊密結合在一起就不足為奇了,REST 的基礎是 HTTP 協議的方法:**HEAD**,**GET**,**POST**,**PUT**,**DELETE** 和 **OPTION**。 例如,`RestTemplate`類具有方法`headForHeaders()`,`getForObject()`,`postForObject()`,`put()`和`delete()`等。 > 閱讀更多和源代碼: [Spring REST 示例 – JSON](https://howtodoinjava.com/spring/spring-restful/spring-rest-hello-world-json-example/) > 閱讀更多:[帶有 HttpClient 的 RestTemplate Java 配置](https://howtodoinjava.com/spring/spring-restful/resttemplate-httpclient-java-config/) ## 1\. Spring `RestTemplate` – HTTP GET 方法示例 #### 1.1. XML 響應 **REST API 代碼** 用于 HTTP GET 方法的 Spring REST API。 ```java @RequestMapping(value = "/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET) public String getAllEmployeesXML(Model model) { model.addAttribute("employees", getEmployeesCollection()); return "xmlTemplate"; } ``` **REST 客戶端代碼** Spring REST 客戶端使用`RestTemplate`訪問 HTTP GET api 請求。 ```java private static void getEmployees() { final String uri = "http://localhost:8080/springrestexample/employees.xml"; RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(uri, String.class); System.out.println(result); } ``` #### 1.2. JSON 響應 **REST API 代碼** ```java @RequestMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET) public String getAllEmployeesJSON(Model model) { model.addAttribute("employees", getEmployeesCollection()); return "jsonTemplate"; } ``` **REST 客戶端代碼** ```java private static void getEmployees() { final String uri = "http://localhost:8080/springrestexample/employees.json"; RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(uri, String.class); System.out.println(result); } ``` #### 1.3. 使用`RestTemplate`的自定義 HTTP 標頭 **REST API 代碼** ```java @RequestMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET) public String getAllEmployeesJSON(Model model) { model.addAttribute("employees", getEmployeesCollection()); return "jsonTemplate"; } ``` **REST 客戶端代碼** ```java private static void getEmployees() { final String uri = "http://localhost:8080/springrestexample/employees"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class); System.out.println(result); } ``` #### 1.4. 獲取響應作為對象 **REST API 代碼** ```java @RequestMapping(value = "/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET) public String getAllEmployeesXML(Model model) { model.addAttribute("employees", getEmployeesCollection()); return "xmlTemplate"; } ``` **REST 客戶端代碼** ```java private static void getEmployees() { final String uri = "http://localhost:8080/springrestexample/employees"; RestTemplate restTemplate = new RestTemplate(); EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class); System.out.println(result); } ``` #### 1.5. URL 參數 **REST API 代碼** ```java @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); } ``` **REST 客戶端代碼** ```java private static void getEmployeeById() { final String uri = "http://localhost:8080/springrestexample/employees/{id}"; Map<String, String> params = new HashMap<String, String>(); params.put("id", "1"); RestTemplate restTemplate = new RestTemplate(); EmployeeVO result = restTemplate.getForObject(uri, EmployeeVO.class, params); System.out.println(result); } ``` ## 2\. Spring `RestTemplate` – HTTP POST 方法示例 **REST API 代碼** 用于 HTTP POST 方法的 Spring REST API。 ```java @RequestMapping(value = "/employees", method = RequestMethod.POST) public ResponseEntity<String> createEmployee(@RequestBody EmployeeVO employee) { System.out.println(employee); return new ResponseEntity(HttpStatus.CREATED); } ``` **REST 客戶端代碼** Spring REST 客戶端使用`RestTemplate`訪問 HTTP POST api 請求。 ```java private static void createEmployee() { final String uri = "http://localhost:8080/springrestexample/employees"; EmployeeVO newEmployee = new EmployeeVO(-1, "Adam", "Gilly", "test@email.com"); RestTemplate restTemplate = new RestTemplate(); EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class); System.out.println(result); } ``` ## 3\. Spring `RestTemplate` – HTTP PUT 方法示例 **REST API 代碼** 用于 HTTP PUT 方法的 Spring REST API。 ```java @RequestMapping(value = "/employees/{id}", method = RequestMethod.PUT) public ResponseEntity<EmployeeVO> updateEmployee(@PathVariable("id") int id, @RequestBody EmployeeVO employee) { System.out.println(id); System.out.println(employee); return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK); } ``` **REST 客戶端代碼** Spring REST 客戶端使用`RestTemplate`訪問 HTTP PUT api 請求。 ```java private static void updateEmployee() { final String uri = "http://localhost:8080/springrestexample/employees/{id}"; Map<String, String> params = new HashMap<String, String>(); params.put("id", "2"); EmployeeVO updatedEmployee = new EmployeeVO(2, "New Name", "Gilly", "test@email.com"); RestTemplate restTemplate = new RestTemplate(); restTemplate.put ( uri, updatedEmployee, params); } ``` ## 4\. Spring `RestTemplate` – HTTP DELETE 方法示例 **REST API 代碼** 用于 HTTP DELETE 方法的 Spring REST API。 ```java @RequestMapping(value = "/employees/{id}", method = RequestMethod.DELETE) public ResponseEntity<String> updateEmployee(@PathVariable("id") int id) { System.out.println(id); return new ResponseEntity(HttpStatus.OK); } ``` **REST 客戶端代碼** Spring REST 客戶端使用`RestTemplate`訪問 HTTP DELETE api 請求。 ```java private static void deleteEmployee() { final String uri = "http://localhost:8080/springrestexample/employees/{id}"; Map<String, String> params = new HashMap<String, String>(); params.put("id", "2"); RestTemplate restTemplate = new RestTemplate(); restTemplate.delete ( uri, params ); } ``` 隨意復制和修改以上 **Spring `RestTemplate`**示例,以在您的 MVC 應用程序中構建 **Spring REST 客戶端** ## 5\. 更多的`RestTemplate`示例 [Spring `RestTemplate`基本身份驗證示例](https://howtodoinjava.com/spring-boot2/resttemplate-basicauth-example/) [Spring `RestTemplate`超時配置示例](https://howtodoinjava.com/spring-boot2/resttemplate-timeout-example/) [Spring `RestTemplateBuilder`示例](https://howtodoinjava.com/spring-restful/resttemplate-builder/) [Spring `RestTemplate` – `HttpClient`配置示例](https://howtodoinjava.com/spring-restful/resttemplate-httpclient-java-config/) [Spring Boot `RestTemplate` GET 示例](https://howtodoinjava.com/spring-boot2/resttemplate-get-example/) [Spring Boot `RestTemplate` POST 示例](https://howtodoinjava.com/spring-boot2/resttemplate-post-json-example/) [帶有`RestTemplate`的 Spring Boot JUnit 示例](https://howtodoinjava.com/spring-boot2/spring-boot-junit-resttemplate/) 帶有標頭的 Spring boot `TestRestTemplate` POST 示例 [帶有`RestTemplate`的 Spring `ClientHttpRequestInterceptor`](https://howtodoinjava.com/spring-restful/clienthttprequestinterceptor/) 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看