标签:rcu jdb inf ret work get ntc ptr domain
模拟浏览器向服务器发送请求四种方式:
看本章之前可以先看看第七章 springboot + retrofit
1、myboot2项目
1.1、application.properties
server.port=8081
注意:指定服务器启动端口的有三种方式
1.2、pom.xml
<!-- import lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.8</version> <scope>provided</scope> </dependency>
1.3、com.xxx.secondboot.domain.Hotel
package com.xxx.secondboot.domain; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @Getter @Setter @AllArgsConstructor @NoArgsConstructor public class Hotel { private int id; private String hotelname; }
1.4、com.xxx.secondboot.web.HotelController
@RestController @RequestMapping("/hotel") @Api("HotelController相关api") public class HotelController { @ApiOperation("获取酒店Hotel信息:getHotelInfo") @RequestMapping(value="/getHotelInfo",method=RequestMethod.GET) public Hotel getHotelInfo(@RequestParam("id") int id, @RequestParam("name") String name) { return new Hotel(id, name); } }
说明:上边的接口,就是准备被调用的接口。
2、myboot1项目
2.1、pom.xml
<!-- 引入okhttp --> 2 <dependency> 3 <groupId>com.squareup.okhttp</groupId> 4 <artifactId>okhttp</artifactId> 5 <version>2.7.5</version> 6 </dependency>
2.2、application-dev.properties
service.hotel.url = http://localhost:8081/hotel/getHotelInfo?id=%d&name=%s
注意:这里使用了String.format()特性,使用指定符号作为占位符进行占位。
2.3、application.properties
spring.profiles.active=dev
2.4、com.xxx.firstboot.config.OkHttpClientConfig
package com.xxx.firstboot.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.squareup.okhttp.OkHttpClient; @Configuration public class OkHttpClientConfig { @Bean public OkHttpClient okHttpClient(){ return new OkHttpClient(); } }
说明:建立OkHttpClient单例。
2.5、com.xxx.firstboot.web.AddressController
@RequestMapping(value = "/testokhttp", method = RequestMethod.GET) public String testokhttp(@RequestParam("id") int id, @RequestParam("name") String name) { String url = String.format(HOTEL_URL, id, name); try { Request request = new Request.Builder().url(url).build(); Response response = okHttpClient.newCall(request).execute(); String result = response.body().string(); LOGGER.debug("testokhttp成功,url:‘{}‘,result:‘{}‘", url, result); return result; } catch (IOException e) { LOGGER.error("testokhttp失败,url:‘{}‘", url); e.printStackTrace(); } return ""; }
说明:这里只是同步get方式,关于okhttp的其他用法,参考:http://www.cnblogs.com/ct2011/p/4001708.html
补充:其实上边这种写法还是有问题的,忘记关闭资源,最终代码如下:
@RequestMapping(value = "/testokhttp", method = RequestMethod.GET) public String testokhttp(@RequestParam("id") int id, @RequestParam("name") String name) { String url = String.format(HOTEL_URL, id, name); Response response = null; try { Request request = new Request.Builder().url(url).build(); response = okHttpClient.newCall(request).execute(); String result = response.body().string(); LOGGER.debug("testokhttp成功,url:‘{}‘,result:‘{}‘", url, result); return result; } catch (IOException e) { LOGGER.error("testokhttp失败,url:‘{}‘", url); e.printStackTrace(); }finally { if(response.body()!=null){ try { response.body().close();//一定要关闭,不然会泄露资源 } catch (IOException e) { e.printStackTrace(); } } } return ""; }
2.6、logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <!-- 1、logger 属性: 1)name:用来指定受此logger约束的某一个包或者具体的某一个类 2)level:用来设置打印级别,大小写无关(最常用的几种):DEBUG, INFO, WARN, ERROR 2、 --> <!-- <logger name="org.springframework.web" level="DEBUG" /> --> <!-- mybatis日志打印 --> <!-- <logger name="com.ibatis" level="DEBUG" /> --> <!-- <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" /> --> <!-- <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" /> --> <!-- <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" /> --> <!-- <logger name="java.sql.Connection" level="DEBUG" /> --> <!-- <logger name="java.sql.Statement" level="DEBUG" /> --> <!-- <logger name="java.sql.PreparedStatement" level="DEBUG" /> --> <!-- 这一句至关重要如果没有,就无法输出sql语句 --> <logger name="com.xxx.firstboot.mapper" level="DEBUG"></logger> <logger name="com.xxx.firstboot.web" level="DEBUG"></logger> </configuration>
第十六章 springboot + OKhttp + String.format
标签:rcu jdb inf ret work get ntc ptr domain
原文地址:http://www.cnblogs.com/sunny3096/p/7160134.html