天天看点

springboot2.0, 使用RestController + RestClient 方式通过API 返回

  1. 目的:使用RestController + RestClient 方式通过API 返回。
  2. 一句话总结:RestController + RestCliet 方式相当于轻量级 WebService。
  3. 客户端技巧
    1. 通过单一的orderId获得返回
    2. 构造map参数,通过map对象获取
    3. 通过map对象获取,并获得HTTP报文头判断后有效返回
  4. 服务器端代码:

    package com.winter.controller;

    import com.winter.model.DeptDomain;
     import com.winter.service.user.DeptService;
     import com.winter.service.user.UserService;
     import lombok.extern.slf4j.Slf4j;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.transaction.annotation.Transactional;
     import org.springframework.web.bind.annotation.PathVariable;
     import org.springframework.web.bind.annotation.RequestMapping;
     import org.springframework.web.bind.annotation.RequestMethod;
    
     import javax.servlet.http.HttpServletRequest;
    
     @org.springframework.web.bind.annotation.RestController
     @RequestMapping(value = "/api/v1")
     @Slf4j
     @Transactional
     public class RestController {
         @Autowired
         private DeptService DeptService;
    
         Logger logger = LoggerFactory.getLogger(RestController.class);
    
         //POST http://localhost:8080/api/v1/order/15
         //获取param
         @RequestMapping(value = "/order/{orderId}", method = RequestMethod.GET)
         public DeptDomain getDeptDomain(@PathVariable int orderId) throws Exception {
     	System.out.println("orderId:>>>" + orderId);
     	DeptDomain deptDomain = DeptService.selectById(orderId);
     	System.out.println(deptDomain.getDeptName());
    
     	return deptDomain;
         }
     }
               
  5. 客户端代码

    package com.winter.controller;

    import com.winter.model.DeptDomain;
     import lombok.extern.slf4j.Slf4j;
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.beans.factory.annotation.Value;
     import org.springframework.boot.web.client.RestTemplateBuilder;
     import org.springframework.context.annotation.Configuration;
     import org.springframework.http.HttpHeaders;
     import org.springframework.http.ResponseEntity;
     import org.springframework.stereotype.Controller;
     import org.springframework.web.bind.annotation.GetMapping;
     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.client.RestTemplate;
    
     import javax.servlet.http.HttpServletRequest;
     import java.util.HashMap;
     import java.util.Map;
    
     @Controller
     @RequestMapping(value = "/test")
     @Slf4j
     @Configuration // 为了配合以下的使用 @Value("${api.order:#{'http://127.0.0.1:8080/api/v1'}}")
     public class RestClientTestController {
         @Autowired
         HttpServletRequest request;
    
     Logger logger = LoggerFactory.getLogger(RestController.class);
    
     //如要如此使用,必须加上@Configuration注释,同时,以下文字必须使用 ' ' 包围文字
     @Value("${api.order:#{'http://127.0.0.1:8080/api/v1'}}")
     String base;
    
     @Autowired
     RestTemplateBuilder restTemplateBuilder;
    
     @GetMapping("/get/{orderId}")
     public @ResponseBody DeptDomain testGetDeptDomain(@PathVariable String orderId)
     throws Exception {
     	RestTemplate restClient = restTemplateBuilder.build();
    
     	//Step 1 构造uri
     	String uri = base + "/order/{orderId}";
     	System.out.println("uri>>>" + uri);
    
     	//Step 2.1 通过单一的orderId获得返回
     	DeptDomain deptDomain1 = restClient.getForObject(uri,DeptDomain.class, orderId);
    
     	//Step 2.2 构造map参数,通过map对象获取
     	Map mapParam = new HashMap();
     	mapParam.put("orderId",orderId);
    
     	DeptDomain deptDomain2 = restClient.getForObject(uri,DeptDomain.class, mapParam);
    
     	//Step 2.2 通过map对象获取,并获得HTTP报文头判断后有效返回
     	ResponseEntity<DeptDomain> responseEntity = restClient.getForEntity(uri,DeptDomain.class, mapParam);
     	System.out.println(responseEntity.getStatusCode());
    
     	if (responseEntity.getStatusCode().is2xxSuccessful()) {
     	    DeptDomain deptDomain3 = responseEntity.getBody();
     	    HttpHeaders headers = responseEntity.getHeaders();
     	    return deptDomain3;
     	} else {
     	    return null;
     	}
     }
               
    }