一、簡介
在Spring-Boot項目開發中,存在着本模塊的代碼需要訪問外面模塊接口,或外部url鏈接的需求,針對這一需求目前存在着三種解決方案,下面將對這三種方案進行整理和說明。
二、Spring-Boot項目中訪問外部接口
2.1 方案一 采用原生的Http請求
在代碼中采用原生的http請求,代碼參考如下:
@RequestMapping("/doPostGetJson")
public String doPostGetJson() throws ParseException {
//此處將要發送的數據轉換為json格式字符串
String jsonText = "{id:1}";
JSONObject json = (JSONObject) JSONObject.parse(jsonText);
JSONObject sr = this.doPost(json);
System.out.println("返回參數:" + sr);
return sr.toString();
}
public static JSONObject doPost(JSONObject date) {
HttpClient client = HttpClients.createDefault();
// 要調用的接口方法
String url = "http://192.168.1.101:8080/getJson";
HttpPost post = new HttpPost(url);
JSONObject jsonObject = null;
try {
StringEntity s = new StringEntity(date.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
post.addHeader("content-type", "text/xml");
HttpResponse res = client.execute(post);
String response1 = EntityUtils.toString(res.getEntity());
System.out.println(response1);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
jsonObject = JSONObject.parseObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return jsonObject;
}
2.2 方案二 采用Feign進行消費
1、在maven項目中添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
2、編寫接口,放置在service層
@FeignClient(url = "${decisionEngine.url}",name="engine")
public interface DecisionEngineService {
@RequestMapping(value="/decision/person",method= RequestMethod.POST)
public JSONObject getEngineMesasge(@RequestParam("uid") String uid,@RequestParam("productCode") String productCode);
}
這里的decisionEngine.url 是配置在properties中的 是ip地址和端口號
decisionEngine.url=http://10.2.1.148:3333
/decision/person 是接口名字
3、在Java的啟動類上加上@EnableFeignClients
@EnableFeignClients //參見此處
@EnableDiscoveryClient
@SpringBootApplication
@EnableResourceServer
public class Application implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
@Autowired
private AppMetricsExporter appMetricsExporter;
@Autowired
private AddMonitorUnitService addMonitorUnitService;
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4、在代碼中調用接口即可
@Autowired
private DecisionEngineService decisionEngineService ;
decisionEngineService.getEngineMesasge("uid" , "productCode");
2.3、方案三 采用RestTemplate方法
在Spring-Boot開發中,RestTemplate同樣提供了對外訪問的接口API,這里主要介紹Get和Post方法的使用。Get請求提供了兩種方式的接口getForObject 和 getForEntity,getForEntity提供如下三種方法的實現。
Get請求之——getForEntity(Stringurl,Class responseType,Object…urlVariables)
該方法提供了三個參數,其中url為請求的地址,responseType為請求響應body的包裝類型,urlVariables為url中的參數綁定,該方法的參考調用如下:
//http://USER-SERVICE/user?name={1}
getForEntity("http://USER-SERVICE/user?name={1}",String.class,"didi")
Get請求之——getForEntity(String url,Class responseType,Map urlVariables)
該方法提供的參數中urlVariables的參數類型使用了Map類型,因此在使用該方法進行參數綁定時需要在占位符中指定Map中參數的key值,該方法的參考調用如下:
// http://USER-SERVICE/user?name={name)
RestTemplate restTemplate=new RestTemplate();
Map<String,String> params=new HashMap<>();
params.put("name","dada"); //
ResponseEntity<String> responseEntity=restTemplate.getForEntity("http://USERSERVICE/user?name={name}",String.class,params);
Get請求之——getForEntity(URI url,Class responseType)
該方法使用URI對象來替代之前的url和urlVariables參數來指定訪問地址和參數綁定。URI是JDK java.net包下的一個類,表示一個統一資源標識符(Uniform Resource Identifier)引用。參考如下:
RestTemplate restTemplate=new RestTemplate();
UriComponents uriComponents=UriComponentsBuilder.fromUriString(
"http://USER-SERVICE/user?name={name}")
.build()
.expand("dodo")
.encode();
URI uri=uriComponents.toUri();
ResponseEntity<String> responseEntity=restTemplate.getForEntity(uri,String.class).getBody();
Get請求之——getForObject
getForObject方法可以理解為對getForEntity的進一步封裝,它通過HttpMessageConverterExtractor對HTTP的請求響應體body內容進行對象轉換,實現請求直接返回包裝好的對象內容。getForObject方法有如下:
getForObject(String url,Class responseType,Object...urlVariables)
getForObject(String url,Class responseType,Map urlVariables)
getForObject(URI url,Class responseType)
Post請求提供有三種方法,postForEntity、postForObject和postForLocation。其中每種方法都存在三種方法,postForEntity方法使用如下:
RestTemplate restTemplate=new RestTemplate();
User user=newUser("didi",30);
ResponseEntity<String> responseEntity=restTemplate.postForEntity("http://USER-SERVICE/user",user,String.class); //提交的body內容為user對象,請求的返回的body類型為String
String body=responseEntity.getBody();
postForEntity存在如下三種方法的重載
postForEntity(String url,Object request,Class responseType,Object... uriVariables)
postForEntity(String url,Object request,Class responseType,Map uriVariables)
postForEntity(URI url,Object request,Class responseType)
postForEntity中的其它參數和getForEntity的參數大體相同在此不做介紹。
原文鏈接:https://blog.csdn.net/polo2044/article/details/85002282
====================SpringBoot接口Http協議開發實戰 =============================
1、SpringBoot2.xHTTP請求配置講解
簡介:SpringBoot2.xHTTP請求注解講解和簡化注解配置技巧
1、@RestController and @RequestMapping是springMVC的注解,不是springboot特有的
2、@RestController = @Controller+@ResponseBody
3、@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan
localhost:8080
2、開發接口必備工具之PostMan接口調試工具介紹和使用
簡介:模擬Http接口測試工具PostMan安裝和講解
1、接口調試工具安裝和基本使用
2、下載地址:https://www.getpostman.com/
3、SpringBoot基礎HTTP接口GET請求實戰
簡介:講解springboot接口,http的get請求,各個注解使用
1、GET請求
1、單一參數@RequestMapping(path = "/{id}", method = RequestMethod.GET)
1) public String getUser(@PathVariable String id ) {}
2)@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 可以同時指定多個提交方法
getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)
3)一個頂倆
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
4)@RequestParam(value = "name", required = true)
可以設置默認值,比如分頁
4)@RequestBody 請求體映射實體類
需要指定http頭為 content-type為application/json charset=utf-8
5)@RequestHeader 請求頭,比如鑒權
@RequestHeader("access_token") String accessToken
6)HttpServletRequest request自動注入獲取參數
4、SpringBoot基礎HTTP接口POST,PUT,DELETE請求實戰
簡介:講解http請求post,put, delete提交方式
GET請求:一般是查詢
Post請求:一般是提交
PUT請求:一般是更新
DELETE請求:一般是刪除
表單驗證或者登陸請求的時候可以用:
//params的類型是map類型
@PostMapping("/v1/login")
public Object login(String id,String pwd){
param.clear();
param.put("id",id);
param.put("pwd",pwd);
return params;
}
上述的方法就可以拿到登陸的密碼和用戶,這樣就可以和數據庫的密碼和用戶進行對比。
5、常用json框架介紹和Jackson返回結果處理
簡介:介紹常用json框架和注解的使用,自定義返回json結構和格式
1、常用框架 阿里 fastjson,谷歌gson等
JavaBean序列化為Json,性能:Jackson > FastJson > Gson > Json-lib 同個結構
測試方法:循環序列化百萬次,次數達到一定才可以看出差別
Jackson、FastJson、Gson類庫各有優點,各有自己的專長
空間換時間,時間換空間
2、jackson處理相關自動
指定字段不返回(比如密碼不應該返回給前端,故用這個注解來注釋不讓密碼進行序列化后的顯示):@JsonIgnore
指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
空字段不返回:@JsonInclude(Include.NON_NUll)
指定別名:@JsonProperty(@JsonProperty("account")盡可能用別名,為了不暴露數據庫的字段,防止黑客的攻擊)
6、SpringBoot2.x目錄文件結構講解
簡介:講解SpringBoot目錄文件結構和官方推薦的目錄規范
1、目錄講解
src/main/java:存放代碼
src/main/resources
static: 存放靜態文件,比如 css、js、image, (訪問方式 http://localhost:8080/js/main.js)
templates:存放靜態頁面jsp,html,tpl
config:存放配置文件,application.properties
resources:
2、引入依賴 Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
注意:如果不引人這個依賴包,html文件應該放在默認加載文件夾里面,
比如resources、static、public這個幾個文件夾,才可以訪問
3、同個文件的加載順序,靜態資源文件
Spring Boot 默認會挨個從
META/resources > resources > static > public 里面找是否存在相應的資源,如果有則直接返回。
4、默認配置
1)官網地址:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content
2)spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
5、靜態資源文件存儲在CDN
7、SpringBoot2.x文件上傳實戰
簡介:講解HTML頁面文件上傳和后端處理實戰
1、講解springboot文件上傳 MultipartFile file,源自SpringMVC
1)靜態頁面直接訪問:localhost:8080/index.html
注意點:
如果想要直接訪問html頁面,則需要把html放在springboot默認加載的文件夾下面
2)MultipartFile 對象的transferTo方法,用於文件保存(效率和操作比原先用FileOutStream方便和高效)
訪問路徑 http://localhost:8080/images/39020dbb-9253-41b9-8ff9-403309ff3f19.jpeg
8、jar包方式運行web項目的文件上傳和訪問處理(核心知識)
簡介:講解SpingBoot2.x使用 java -jar運行方式的圖片上傳和訪問處理
1、文件大小配置,啟動類里面配置
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//單個文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 設置總上傳數據總大小
factory.setMaxRequestSize("1024000KB");
return factory.createMultipartConfig();
}
2、打包成jar包,需要增加maven依賴
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果沒加相關依賴,執行maven打包,運行后會報錯:no main manifest attribute, in XXX.jar
GUI:反編譯工具,作用就是用於把class文件轉換成java文件
3、文件上傳和訪問需要指定磁盤路徑
application.properties中增加下面配置
1) web.images-path=/Users/jack/Desktop
2) spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path}
4、文件服務器:fastdfs,阿里雲oss,nginx搭建一個簡單的文件服務器
————————————————
原文鏈接:https://blog.csdn.net/u012429555/article/details/81276261
SpringBoot配置HTTPS,並實現HTTP訪問自動轉HTTPS訪問
1.使用jdk自帶的 keytools 創建證書
打開cmd窗口,輸入如下命令
keytool -genkey -alias tomcat -keyalg RSA -keystore ./server.keystore
按照提示進行操作
輸入密鑰庫口令:123456 再次輸入新口令:123456 您的名字與姓氏是什么? [Unknown]: kaibowang 您的組織單位名稱是什么? [Unknown]: yuxuelian 您的組織名稱是什么? [Unknown]: yuxuelian 您所在的城市或區域名稱是什么? [Unknown]: chengdu 您所在的省/市/自治區名稱是什么? [Unknown]: chengdushi 該單位的雙字母國家/地區代碼是什么? [Unknown]: china CN=kaibowang, OU=yuxuelian, O=yuxuelian, L=chengdu, ST=chengdushi, C=china是否正確? [否]: y 輸入 <tomcat> 的密鑰口令 (如果和密鑰庫口令相同, 按回車): 再次輸入新口令: Warning: JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore C:\Users\Administrator\.keystore -destkeystore C:\Users\Administrator\.keystore -deststoretype pkcs12" 遷移到行業標准格式 PKCS12。
創建完成后,可在用戶根目錄查看生成的keystore文件
2.新建springboot項目,將上一步生成的keystone文件復制到項目的根目錄,在application.properties添加如下配置
server.port=443 server.ssl.key-store=server.keystore server.ssl.key-alias=tomcat server.ssl.enabled=true server.ssl.key-store-password=123456 server.ssl.key-store-type=JKS
說明一下
這里將服務器端口號設置成443端口,即https的默認訪問端口,那么在進行https訪問的時候可以不帶端口號直接訪問,如果端口被占用使用
netstat -ano
查看哪個進程號占用了端口,使用
tasklist|findstr (查看到的進程號)
# simple
C:\Users\Administrator>tasklist|findstr 3664
vmware-hostd.exe 3664 Services 0 5,040 K
打開任務管理器,殺死占用進程,或打開對應的應用程序的設置,關閉監聽
至此 https配置完畢 訪問 https://localhost 查看是否配置成功
3.http訪問自動轉https訪問
向spring容器中注入兩個Bean,代碼如下
@Bean public Connector connector(){ Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(80); connector.setSecure(false); connector.setRedirectPort(443); return connector; } @Bean public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){ TomcatServletWebServerFactory tomcat=new TomcatServletWebServerFactory(){ @Override protected void postProcessContext(Context context) { SecurityConstraint securityConstraint=new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection=new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(connector); return tomcat; }
首先 這里需要使用 TomcatServletWebServerFactory 這個類,網上清一色的都是使用 EmbeddedServletContainerFactory 這個類.
在新版本的SpringBoot中,我發現已近找不到這個類了,幾經周轉,翻閱源碼,才找到這個類,這也是我為什么寫這篇文章的初衷.
其次在這里設置http的監聽端口為80端口,http默認端口,這樣在訪問的時候也可以不用帶上端口號.
完成以上配置后,我們訪問 http://localhost 即可自動跳轉為 https://localhost
原文鏈接:https://www.jianshu.com/p/8d4aba3b972d
SpringBoot Web Https 配置
不管是游戲服務器開發,還是其它服務開發,越來越多的平台都要求服務端必須支持https的訪問。以增加安全性。比如目前火熱的小程序,要求服務端必須支持https,蘋果商店也有說http請求要修改為https。所以https將會是游戲服務器的普遍需求。
一,證書生成
證書可以自己使用jdk生成進行測試。但是在正常使用的時候,需要去第三方機構購買,網上也有免費的。不過有效期有限制。具體獲取證書的方法這里不再詳細說明了。一般拿到證書之后會得到這幾個文件:
cert.pem chain.pem fullchain.pem privkey.pem
二,將pem文件轉化為keystore文件
如果使用nginx跳轉的話,上面的證書文件可以直接使用,但是在tomcat中,證書的配置文件格式必須是.keystore的文件。所以需要做一下轉化。
1、生成pkcs12格式的密鑰文件:
$ openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out my.pk12 -name mykey
(注:此過程中需要輸入密碼:123456)
2、生成keystore:
$ keytool -importkeystore -deststorepass 123456 -destkeypass 123456 -destkeystore my.keystore -srckeystore my.pk12 -srcstoretype PKCS12 -srcstorepass 123456 -alias shkey
成功之后會獲得my.keystore文件。
三,在Spring boot web中配置https
首先是在application.properties中添加配置
1
2
3
4
5
|
server.port=
8446
server.ssl.key-store=/user/cert/my.keystore
server.ssl.key-store-password=
123456
|
這樣配置之后,啟動服務,就可以https訪問了。
四,同時支持http和https訪問
1,http請求不跳轉成https訪問
這種方式是http請求單獨走一個端口,https請求單獨走一個端口。但是spring boot 的appplication.properties只能配置一個端口,這就需要我們手動再添加一個Connector了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@Configuration
public
class
TomcatConfig {
@Bean
public
EmbeddedServletContainerFactory servletContainerFactory(){
TomcatEmbeddedServletContainerFactory tomcatConfig =
new
TomcatEmbeddedServletContainerFactory();
tomcatConfig.addAdditionalTomcatConnectors(
this
.newHttpConnector());
return
tomcatConfig;
}
private
Connector newHttpConnector() {
Connector connector =
new
Connector(
"org.apache.coyote.http11.Http11NioProtocol"
);
connector.setScheme(
"http"
);
connector.setPort(
8080
);
connector.setSecure(
false
);
return
connector;
}
}
|
這樣普通 的http請求,可以訪問8080端口了。
2,將http請求強制跳轉到https
有時候我們的一些舊業務是使用的http,但是新業務以及將來的框架都必須強制使用https,那就需要做一下跳轉,把收到的http請求強制跳轉到https上面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
@Configuration
public
class
TomcatConfig {
@Bean
public
EmbeddedServletContainerFactory servletContainerFactory(){
TomcatEmbeddedServletContainerFactory tomcatConfig =
new
TomcatEmbeddedServletContainerFactory(){
@Override
protected
void
postProcessContext(Context context) {
SecurityConstraint securityConstraint =
new
SecurityConstraint();
securityConstraint.setUserConstraint(
"CONFIDENTIAL"
);
SecurityCollection collection =
new
SecurityCollection();
// 這里不知道為什么,只能配置以/*結尾的path。這樣配置表示全部請求使用安全模式,必須走https
collection.addPattern(
"/*"
);
//另外還可以配置哪些請求必須走https,這表示以/home/開頭的請求必須走https
collection.addPattern(
"/home/*"
);
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcatConfig.addAdditionalTomcatConnectors(
this
.newHttpConnector());
return
tomcatConfig;
}
private
Connector newHttpConnector() {
Connector connector =
new
Connector(
"org.apache.coyote.http11.Http11NioProtocol"
);
connector.setScheme(
"http"
);
connector.setPort(
8080
);
connector.setSecure(
false
);
// 如果只需要支持https訪問,這里把收到的http請求跳轉到https的端口
connector.setRedirectPort(
8446
);
return
connector;
}
}
|
以上跳轉也可以使用nginx實現。如果有什么問題可以評論留言或加QQ群:66728073交流