模拟浏览器向服务器发送请求四种方式:
- jdk原生的Http包下的一些类
- httpclient(比较原始,不怎么用了):
- Okhttp(好用,推荐)
- retrofit(好用,推荐),用法:
看本章之前可以先看看
1、myboot2项目
1.1、application.properties
1 server.port=8081
注意:指定服务器启动端口的有三种方式
- 在application.properties文件中配置server.port = xxx(xxx为端口,eg.8081)(最推荐)
- 启动类实现EmbeddedServletContainerCustomizer接口,并重写其方法,参考
- 打好jar后,"java -jar xx.jar --server.port=8081"
1.2、pom.xml
1 23 org.projectlombok 4lombok 51.16.8 6provided 7
1.3、com.xxx.secondboot.domain.Hotel
1 package com.xxx.secondboot.domain; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Getter; 5 import lombok.NoArgsConstructor; 6 import lombok.Setter; 7 8 @Getter @Setter 9 @AllArgsConstructor @NoArgsConstructor10 public class Hotel {11 private int id;12 private String hotelname;13 }
1.4、com.xxx.secondboot.web.HotelController
1 @RestController 2 @RequestMapping("/hotel") 3 @Api("HotelController相关api") 4 public class HotelController { 5 6 @ApiOperation("获取酒店Hotel信息:getHotelInfo") 7 @RequestMapping(value="/getHotelInfo",method=RequestMethod.GET) 8 public Hotel getHotelInfo(@RequestParam("id") int id, @RequestParam("name") String name) { 9 return new Hotel(id, name);10 }11 }
说明:上边的接口,就是准备被调用的接口。
2、myboot1项目
2.1、pom.xml
1 23 com.squareup.okhttp 4okhttp 52.7.5 6
2.2、application-dev.properties
1 service.hotel.url = http://localhost:8081/hotel/getHotelInfo?id=%d&name=%s
注意:这里使用了String.format()特性,使用指定符号作为占位符进行占位。
- 关于占位符的类型参考:
- 一定要注意,是%d,而不是d%
2.3、application.properties
1 spring.profiles.active=dev
2.4、com.xxx.firstboot.config.OkHttpClientConfig
1 package com.xxx.firstboot.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 6 import com.squareup.okhttp.OkHttpClient; 7 8 @Configuration 9 public class OkHttpClientConfig {10 11 @Bean12 public OkHttpClient okHttpClient(){13 return new OkHttpClient();14 }15 }
说明:建立OkHttpClient单例。
2.5、com.xxx.firstboot.web.AddressController
1 @RequestMapping(value = "/testokhttp", method = RequestMethod.GET) 2 public String testokhttp(@RequestParam("id") int id, @RequestParam("name") String name) { 3 String url = String.format(HOTEL_URL, id, name); 4 try { 5 Request request = new Request.Builder().url(url).build(); 6 Response response = okHttpClient.newCall(request).execute(); 7 String result = response.body().string(); 8 LOGGER.debug("testokhttp成功,url:'{}',result:'{}'", url, result); 9 return result;10 } catch (IOException e) {11 LOGGER.error("testokhttp失败,url:'{}'", url);12 e.printStackTrace();13 }14 return "";15 }
说明:这里只是同步get方式,关于okhttp的其他用法,参考:
补充:其实上边这种写法还是有问题的,忘记关闭资源,最终代码如下:
1 @RequestMapping(value = "/testokhttp", method = RequestMethod.GET) 2 public String testokhttp(@RequestParam("id") int id, @RequestParam("name") String name) { 3 String url = String.format(HOTEL_URL, id, name); 4 Response response = null; 5 try { 6 Request request = new Request.Builder().url(url).build(); 7 response = okHttpClient.newCall(request).execute(); 8 String result = response.body().string(); 9 LOGGER.debug("testokhttp成功,url:'{}',result:'{}'", url, result);10 return result;11 } catch (IOException e) {12 LOGGER.error("testokhttp失败,url:'{}'", url);13 e.printStackTrace();14 }finally {15 if(response.body()!=null){16 try {17 response.body().close();//一定要关闭,不然会泄露资源18 } catch (IOException e) {19 e.printStackTrace();20 }21 }22 }23 return "";24 }
2.6、logback.xml
1 23 4 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24