Chapter 2 springboot + OKhttp + String. format, springbootokhttp
Four methods are used to simulate a browser to send a request to the server:
- Some classes under the jdk native Http package
- Httpclient (relatively primitive and rarely used): Chapter 1 Use of HttpClient
- Okhttp(Easy to use,Recommended)
- Retrofit(Easy to use,Recommendation), Usage: Chapter 7 springboot + Dynamic Fit
Before reading this chapter, you can take a look at Chapter 7 springboot + starter fit.
1. myboot2 Project
1.1. application. properties
1 server. port = 8081View Code
Note: There are three methods to specify the server startup port.
- Configure in the application. properties FileServer. port = xxx(Xxx is the port, eg.8081) (the mostRecommendation)
- Enable the class to implement the EmbeddedServletContainerCustomizer interface and rewrite the method. For details, refer to Chapter 7 springboot + retrofit.
- After jar is prepared, "java-jar xx. jar -- server. port = 8081"
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 Code
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 comment name; 13}View Code
1.4. com. xxx. secondboot. web. Alibaba Controller
1 @ RestController 2 @ RequestMapping ("/hotel") 3 @ Api ("container Controller related api") 4 public class guest controller {5 6 @ ApiOperation ("Get Hotel information: getaskinfo ") 7 @ RequestMapping (value ="/getaskinfo ", method = RequestMethod. GET) 8 public Hotel getaskinfo (@ RequestParam ("id") int id, @ RequestParam ("name") String name) {9 return new Hotel (id, name ); 10} 11}View Code
Note: The above interface is the interface to be called.
2. myboot1 Project
2.1. pom. xml
1 <! -- Introduce okhttp --> 2 <dependency> 3 <groupId> com. squareup. okhttp </groupId> 4 <artifactId> okhttp </artifactId> 5 <version> 2.7.5 </version> 6 </dependency>View Code
Application-dev.properties 2.2
1 service. hotel. url = http: // localhost: 8081/hotel/get1_info? Id = % d & name = % sView Code
Note:String. format ()Feature, using the specified symbol as a placeholder for placeholder.
- Type reference for placeholders: http://blog.csdn.net/lonely_fireworks/article/details/7962171
- Be sure to note that it is % d, not d %
2.3. application. properties
1 spring. profiles. active = devView Code
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}View Code
Note: Create an OkHttpClient Singleton.
2.5. com. xxx. firstboot. web. AddressController
1 @ RequestMapping (value = "/tew.http", method = RequestMethod. GET) 2 public String tew.http (@ RequestParam ("id") int id, @ RequestParam ("name") String name) {3 String url = String. format (pai_url, id, name); 4 try {5 Request request = new Request. builder (). url (url ). build (); 6 Response response = okhttpclient.newcall(requestcmd.exe cute (); 7 String result = response. body (). string (); 8 LOGGER. debug ("tew.http succeeded, url: '{}', result: '{}'", url, result); 9 return result; 10} catch (IOException e) {11 LOGGER. error ("tew.http failed, url: '{}'", url); 12 e. printStackTrace (); 13} 14 return ""; 15}View Code
(Here only synchronous get method, other usage of okhttp, refer to: 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 attributes: 6 1) name: used to specify a package or a specific class bound by this logger. 7) level: used to set the print level, case-insensitive (most commonly used): DEBUG, INFO, WARN, ERROR 8 2, 9 --> 10 <! -- <Logger name = "org. springframework. web" level = "DEBUG"/> --> 11 12 <! -- Mybatis log printing --> 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 <! -- This sentence is crucial. If not, the SQL statement cannot be output --> 22 <logger name = "com. xxx. firstboot. mapper "level =" DEBUG "> </logger> 23 <logger name =" com. xxx. firstboot. web "level =" DEBUG "> </logger> 24 </configuration>View Code