星空网 > 软件开发 > Java

第十六章 springboot + OKhttp + String.format

模拟浏览器向服务器发送请求四种方式:

  • jdk原生的Http包下的一些类
  • httpclient(比较原始,不怎么用了):第一章 HttpClient的使用
  • Okhttp(好用,推荐)
  • retrofit(好用,推荐),用法:第七章 springboot + retrofit

看本章之前可以先看看第七章 springboot + retrofit

1、myboot2项目

1.1、application.properties

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
1 server.port=8081

View Code

注意:指定服务器启动端口的有三种方式

  • 在application.properties文件中配置server.port = xxx(xxx为端口,eg.8081)(最推荐)
  • 启动类实现EmbeddedServletContainerCustomizer接口,并重写其方法,参考第七章 springboot + retrofit
  • 打好jar后,"java -jar xx.jar --server.port=8081"

1.2、pom.

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
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

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
 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 }

View Code

1.4、com.xxx.secondboot.web.HotelController

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
 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.

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
1     <!-- 引入okhttp -->2     <dependency>3       <groupId>com.squareup.okhttp</groupId>4       <artifactId>okhttp</artifactId>5       <version>2.7.5</version>6     </dependency>

View Code

2.2、application-dev.properties

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
1 service.hotel.url = http://localhost:8081/hotel/getHotelInfo?id=%d&name=%s

View Code

注意:这里使用了String.format()特性,使用指定符号作为占位符进行占位。

  • 关于占位符的类型参考:http://blog.csdn.net/lonely_fireworks/article/details/7962171
  • 一定要注意,是%d,而不是d%

2.3、application.properties

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
1 spring.profiles.active=dev

View Code

2.4、com.xxx.firstboot.config.OkHttpClientConfig

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
 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

说明:建立OkHttpClient单例。

2.5、com.xxx.firstboot.web.AddressController

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
 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.

第十六章 springboot + OKhttp + String.format第十六章 springboot + OKhttp + String.format
 1 <??> 2 <configuration> 3   <include resource="org/springframework/boot/logging/logback/base. /> 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



原标题:第十六章 springboot + OKhttp + String.format

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok 国外版:https://www.goluckyvip.com/tag/83342.html
tiktok 零播放:https://www.goluckyvip.com/tag/83343.html
tiktok dl:https://www.goluckyvip.com/tag/83344.html
tiktok 卡:https://www.goluckyvip.com/tag/83345.html
tiktok 登陆:https://www.goluckyvip.com/tag/83346.html
tiktok 商家登录网址:https://www.goluckyvip.com/tag/83348.html
深圳到西安自驾路线攻略 深圳到西安自驾最佳路线:https://www.vstour.cn/a/411228.html
松花蛋是哪里的特产松花蛋的产地:https://www.vstour.cn/a/411229.html
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流