總結:Spring Boot

Spring Boot - 訪問外部接口

在Spring-Boot項目開發中,存在著本模塊的代碼需要訪問外面模塊接口,或外部url鏈接的需求, 比如調用外部的地圖API或者天氣API。

Spring Boot - 訪問外部接口方案一: 採用原生的Http請求方案二: 採用Feign進行消費方案三: 採用RestTemplate方法Get請求之——getForEntity(Stringurl,Class responseType,Object…urlVariables)Get請求之——getForEntity(URI url,Class responseType)Get請求之——getForObjectPost 請求


方案一: 採用原生的Http請求

在代碼中採用原生的http請求,代碼參考如下:

<code>@RequestMapping("/doPostGetJson")public String doPostGetJson() throws ParseException { //此處將要發送的數據轉換為json格式字符串 String jsonText = "{id:1}"; JSONObject json = (JSONObject) JSONObject.parse(jsonText); JSONObject sr = this.doPost(json); System.out.println("返回參數:" + sr); return sr.toString();}public static JSONObject doPost(JSONObject date) { HttpClient client = HttpClients.createDefault(); // 要調用的接口方法 String url = "http://192.168.1.101:8080/getJson"; HttpPost post = new HttpPost(url); JSONObject jsonObject = null; try { StringEntity s = new StringEntity(date.toString()); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); post.setEntity(s); post.addHeader("content-type", "text/xml"); HttpResponse res = client.execute(post); String response1 = EntityUtils.toString(res.getEntity()); System.out.println(response1); if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String result = EntityUtils.toString(res.getEntity());// 返回json格式: jsonObject = JSONObject.parseObject(result); } } catch (Exception e) { throw new RuntimeException(e); } return jsonObject;}/<code>

方案二: 採用Feign進行消費

1、在maven項目中添加依賴

<code><dependency> <groupid>org.springframework.cloud/<groupid> <artifactid>spring-cloud-starter-feign/<artifactid> <version>1.2.2.RELEASE/<version>/<dependency>/<code>

2、編寫接口,放置在service層

這裡的decisionEngine.url 是配置在properties中的 是ip地址和端口號
decisionEngine.url=http://10.2.1.148:3333/decision/person 是接口名字

<code>@FeignClient(url = "${decisionEngine.url}",name="engine")public interface DecisionEngineService {  @RequestMapping(value="/decision/person",method= RequestMethod.POST)  public JSONObject getEngineMesasge(@RequestParam("uid") String uid,@RequestParam("productCode") String productCode);}/<code>

3、在Java的啟動類上加上@EnableFeignClients

<code>@EnableFeignClients //參見此處@EnableDiscoveryClient@SpringBootApplication@EnableResourceServerpublic class Application implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); @Autowired private AppMetricsExporter appMetricsExporter; @Autowired private AddMonitorUnitService addMonitorUnitService; public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }/<code>

4、在代碼中調用接口即可

<code>@Autowiredprivate DecisionEngineService decisionEngineService ;// ...decisionEngineService.getEngineMesasge("uid" , "productCode");/<code>

方案三: 採用RestTemplate方法

在Spring-Boot開發中,RestTemplate同樣提供了對外訪問的接口API,這裡主要介紹Get和Post方法的使用。Get請求提供了兩種方式的接口getForObject 和 getForEntity,getForEntity提供如下三種方法的實現。

Get請求之——getForEntity(Stringurl,Class responseType,Object…urlVariables)

該方法提供了三個參數,其中url為請求的地址,responseType為請求響應body的包裝類型,urlVariables為url中的參數綁定,該方法的參考調用如下:

<code>// http://USER-SERVICE/user?name={name)RestTemplate restTemplate=new RestTemplate();Map<string> params=new HashMap<>();params.put("name","dada"); //ResponseEntity<string> responseEntity=restTemplate.getForEntity("http://USERSERVICE/user?name={name}",String.class,params);/<string>/<string>/<code>

Get請求之——getForEntity(URI url,Class responseType)

該方法使用URI對象來替代之前的url和urlVariables參數來指定訪問地址和參數綁定。URI是JDK http://java.net包下的一個類,表示一個統一資源標識符(Uniform Resource Identifier)引用。參考如下:

<code>RestTemplate restTemplate=new RestTemplate();UriComponents uriComponents=UriComponentsBuilder.fromUriString("http://USER-SERVICE/user?name={name}") .build() .expand("dodo") .encode();URI uri=uriComponents.toUri();ResponseEntity<string> responseEntity=restTemplate.getForEntity(uri,String.class).getBody();/<string>/<code>

Get請求之——getForObject

getForObject方法可以理解為對getForEntity的進一步封裝,它通過HttpMessageConverterExtractor對HTTP的請求響應體body內容進行對象轉換,實現請求直接返回包裝好的對象內容。getForObject方法有如下:

<code>getForObject(String url,Class responseType,Object...urlVariables)getForObject(String url,Class responseType,Map urlVariables)getForObject(URI url,Class responseType)/<code>

Post 請求

Post請求提供有三種方法,postForEntity、postForObject和postForLocation。其中每種方法都存在三種方法,postForEntity方法使用如下:

<code>RestTemplate restTemplate=new RestTemplate();User user=newUser("didi",30);ResponseEntity<string> responseEntity=restTemplate.postForEntity("http://USER-SERVICE/user",user,String.class); //提交的body內容為user對象,請求的返回的body類型為StringString body=responseEntity.getBody();/<string>/<code>

postForEntity存在如下三種方法的重載

<code>postForEntity(String url,Object request,Class responseType,Object... uriVariables)postForEntity(String url,Object request,Class responseType,Map uriVariables)postForEntity(URI url,Object request,Class responseType)/<code>

postForEntity中的其它參數和getForEntity的參數大體相同在此不做介紹。

最後,如果覺得本文不錯,麻煩關注轉發一下,非常感謝大家的支持~