模擬浏覽器向服務器發送請求四種方式:
看本章之前可以先看看第七章 springboot + retrofit
1、myboot2項目
1.1、application.properties
1 server.port=8081 View Code注意:指定服務器啟動端口的有三種方式
1.2、pom.xml
1 <!-- import lombok --> 2 <dependency> 3 <groupId>org.projectlombok</groupId> 4 <artifactId>lombok</artifactId> 5 <version>1.16.8</version> 6 <scope>provided</scope> 7 </dependency> View Code1.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 @NoArgsConstructor 10 public class Hotel { 11 private int id; 12 private String hotelname; 13 } View Code1.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 } View Code說明:上邊的接口,就是准備被調用的接口。
2、myboot1項目
2.1、pom.xml
1 <!-- 引入okhttp --> 2 <dependency> 3 <groupId>com.squareup.okhttp</groupId> 4 <artifactId>okhttp</artifactId> 5 <version>2.7.5</version> 6 </dependency> View Code2.2、application-dev.properties
1 service.hotel.url = http://localhost:8081/hotel/getHotelInfo?id=%d&name=%s View Code注意:這裡使用了String.format()特性,使用指定符號作為占位符進行占位。
2.3、application.properties
1 spring.profiles.active=dev View Code2.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 @Bean 12 public OkHttpClient okHttpClient(){ 13 return new OkHttpClient(); 14 } 15 } View Code說明:建立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 } View Code說明:這裡只是同步get方式,關於okhttp的其他用法,參考:http://www.cnblogs.com/ct2011/p/4001708.html
2.6、logback.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration> 3 <include resource="org/springframework/boot/logging/logback/base.xml" /> 4 <!-- 1、logger 5 屬性: 6 1)name:用來指定受此logger約束的某一個包或者具體的某一個類 7 2)level:用來設置打印級別,大小寫無關(最常用的幾種):DEBUG, INFO, WARN, ERROR 8 2、 9 --> 10 <!-- <logger name="org.springframework.web" level="DEBUG" /> --> 11 12 <!-- mybatis日志打印 --> 13 <!-- <logger name="com.ibatis" level="DEBUG" /> --> 14 <!-- <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" /> --> 15 <!-- <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" /> --> 16 <!-- <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" /> --> 17 18 <!-- <logger name="java.sql.Connection" level="DEBUG" /> --> 19 <!-- <logger name="java.sql.Statement" level="DEBUG" /> --> 20 <!-- <logger name="java.sql.PreparedStatement" level="DEBUG" /> --> 21 <!-- 這一句至關重要如果沒有,就無法輸出sql語句 --> 22 <logger name="com.xxx.firstboot.mapper" level="DEBUG"></logger> 23 <logger name="com.xxx.firstboot.web" level="DEBUG"></logger> 24 </configuration> View Code