一、依赖
4.0.0
com.ub
data-importer2023
1.0.0
17
17
UTF-8
1.54.0
org.springframework.boot
spring-boot-starter-parent
3.0.5
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-webflux
kr.motd.maven
os-maven-plugin
1.7.1
org.xolstice.maven.plugins
protobuf-maven-plugin
0.6.1
com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}
grpc-java
io.grpc:protoc-gen-grpc-java:1.54.0:exe:${os.detected.classifier}
compile
compile-custom
二、定义一个http接口
package com.ub.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/hi")
public String hi(@RequestParam("username") String username,
@RequestParam("password")String password){
return username+","+password;
}
}
三、定义声明式HttpExchange接口
package com.ub.service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;
@HttpExchange
public interface HttpService {
@GetExchange("hi")
String login(@RequestParam("username") String username, @RequestParam("password") String password);
}
四、使用HttpExchange接口调用http服务
package com.ub.controller;
import com.ub.service.HttpService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@RestController
public class HttpController {
@RequestMapping("/test")
public String test(){
WebClient client = WebClient.builder().baseUrl("http://localhost:8088/").build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
HttpService httpService = factory.createClient(HttpService.class);
return httpService.login("tju","fgh");
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net