How to interface with external systems? Three ways for Spring Boot projects to call external interfaces

1 Introduction

SpringBoot not only inherits the original excellent features of the Spring framework, but also further simplifies the entire construction and development process of Spring applications by simplifying configuration.

In the development of the Spring-Boot project, there is a requirement that the code of this module needs to access the external module interface or external url link. For example, in the process of apaas development, it is necessary to encapsulate the interface and call the interface provided by apaas in the interface (such as the process interface submit etc.) The following also provides three ways (without using dubbo) for us to choose

Recommend an open source and free Spring Boot practical project:

https://github.com/javastacks/spring-boot-best-practice

2. Method 1: Use the original httpClient request

/*
 * @description get方式获取入参,插入数据并发起流程
 * @author lyx
 * @date 2022/8/24 16:05
 * @params documentId
 * @return String
 */
//
@RequestMapping("/submit/{documentId}")
public String submit1(@PathVariable String documentId) throws ParseException {
    //此处将要发送的数据转换为json格式字符串
    Map<String,Object> map =task2Service.getMap(documentId);
    String jsonStr = JSON.toJSONString(map, SerializerFeature.WRITE_MAP_NULL_FEATURES,SerializerFeature.QuoteFieldNames);
    JSONObject jsonObject = JSON.parseObject(jsonStr);
    JSONObject sr = task2Service.doPost(jsonObject);
    return sr.toString();
}
/*
 * @description 使用原生httpClient调用外部接口
 * @author lyx
 * @date 2022/8/24 16:08
 * @params date
 * @return JSONObject
 */
public static JSONObject doPost(JSONObject date) {
    String assessToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ4ZGFwYXBwaWQiOiIzNDgxMjU4ODk2OTI2OTY1NzYiLCJleHAiOjE2NjEyMjY5MDgsImlhdCI6MTY2MTIxOTcwOCwieGRhcHRlbmFudGlkIjoiMzAwOTgxNjA1MTE0MDUyNjA5IiwieGRhcHVzZXJpZCI6IjEwMDM0NzY2MzU4MzM1OTc5NTIwMCJ9.fZAO4kJSv2rSH0RBiL1zghdko8Npmu_9ufo6Wex_TI2q9gsiLp7XaW7U9Cu7uewEOaX4DTdpbFmMPvLUtcj_sQ";
    CloseableHttpClient client = HttpClients.createDefault();
    // 要调用的接口url
    String url = "http://39.103.201.110:30661 /xdap-open/open/process/v1/submit";
    HttpPost post = new HttpPost(url);
    JSONObject jsonObject = null;
    try {
        //创建请求体并添加数据
        StringEntity s = new StringEntity(date.toString());
        //此处相当于在header里头添加content-type等参数
        s.setContentType("application/json");
        s.setContentEncoding("UTF-8");
        post.setEntity(s);
        //此处相当于在Authorization里头添加Bear token参数信息
        post.addHeader("Authorization", "Bearer " +assessToken);
        HttpResponse res = client.execute(post);
        String response1 = EntityUtils.toString(res.getEntity());
        if (res.getStatusLine()
                .getStatusCode() == HttpStatus.SC_OK) {
            // 返回json格式:
            String result = EntityUtils.toString(res.getEntity());
            jsonObject = JSONObject.parseObject(result);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return jsonObject;
}

3. Method 2: Use the RestTemplate method

In the development of Spring-Boot, RestTemplatethe interface API for external access is also provided. Here we mainly introduce the use of Get and Post methods.

Get request

getForObject Two methods are provided getForEntity, among which getForEntitythe following three methods are implemented:

Get--getForEntity, there are two ways to overload

1.getForEntity(Stringurl,Class responseType,Object…urlVariables)
2.getForEntity(URI url,Class responseType)

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();
ResponseEntityresponseEntity=restTemplate.getForEntity(uri,String.class).getBody();

Get--getForEntity(Stringurl,Class responseType,Object…urlVariables)

//该方法提供了三个参数,其中url为请求的地址,responseType为请求响应body的包装类型,urlVariables为url中的参数绑定,该方法的参考调用如下:
// http://USER-SERVICE/user?name={name)
RestTemplate restTemplate=new RestTemplate();
Mapparams=new HashMap<>();
params.put("name","dada"); //
ResponseEntityresponseEntity=restTemplate.getForEntity("http://USERSERVICE/user?name={name}",String.class,params);

Get--getForObject, there are three ways to overload

1.getForObject(String url,Class responseType,Object...urlVariables)
2.getForObject(String url,Class responseType,Map urlVariables)
3.getForObject(URI url,Class responseType)

The getForObject method can be understood as a further encapsulation of getForEntity. It HttpMessageConverterExtractorconverts the object content of the HTTP request response body to realize that the request directly returns the packaged object content.

In addition, if you are preparing for an interview to change jobs in the near future, it is recommended to brush up questions online in the Java interview library applet, covering 2000+ Java interview questions, covering almost all mainstream technical interview questions.

Post request

There are three methods of Post request postForEntity, , postForObjectand , and each method has three methods, which are described below .postForLocationpostForEntity

Post--postForEntity, there are three ways to overload

1.postForEntity(String url,Object request,Class responseType,Object...  uriVariables) 
2.postForEntity(String url,Object request,Class responseType,Map  uriVariables) 
3.postForEntity(URI url,Object request,Class responseType)

Only the second overloading method is demonstrated as follows

/*
 * @description post方式获取入参,插入数据并发起流程
 * @author lyx
 * @date 2022/8/24 16:07
 * @params
 * @return
 */
@PostMapping("/submit2")
public Object insertFinanceCompensation(@RequestBody JSONObject jsonObject) {
    String documentId=jsonObject.get("documentId").toString();
    return task2Service.submit(documentId);
}
/*
 * @description 使用restTimeplate调外部接口
 * @author lyx
 * @date 2022/8/24 16:02
 * @params documentId
 * @return String
 */
public String submit(String documentId){
    String assessToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ4ZGFwYXBwaWQiOiIzNDgxMjU4ODk2OTI2OTY1NzYiLCJleHAiOjE2NjEyMjY5MDgsImlhdCI6MTY2MTIxOTcwOCwieGRhcHRlbmFudGlkIjoiMzAwOTgxNjA1MTE0MDUyNjA5IiwieGRhcHVzZXJpZCI6IjEwMDM0NzY2MzU4MzM1OTc5NTIwMCJ9.fZAO4kJSv2rSH0RBiL1zghdko8Npmu_9ufo6Wex_TI2q9gsiLp7XaW7U9Cu7uewEOaX4DTdpbFmMPvLUtcj_sQ";
    RestTemplate restTemplate = new RestTemplate();
    //创建请求头
    HttpHeaders httpHeaders = new HttpHeaders();
    //此处相当于在Authorization里头添加Bear token参数信息
    httpHeaders.add(HttpHeaders.AUTHORIZATION, "Bearer " + assessToken);
    //此处相当于在header里头添加content-type等参数
    httpHeaders.add(HttpHeaders.CONTENT_TYPE,"application/json");
    Map<String, Object> map = getMap(documentId);
    String jsonStr = JSON.toJSONString(map);
    //创建请求体并添加数据
    HttpEntity<Map> httpEntity = new HttpEntity<Map>(map, httpHeaders);
    String url = "http://39.103.201.110:30661/xdap-open/open/process/v1/submit";
    ResponseEntity<String> forEntity = restTemplate.postForEntity(url,httpEntity,String.class);//此处三个参数分别是请求地址、请求体以及返回参数类型
    return forEntity.toString();
}

4. Method 3: Use Feign for consumption

Add dependencies to the maven project

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

Add to the startup class@EnableFeignClients

@SpringBootApplication
@EnableFeignClients
@ComponentScan(basePackages = {"com.definesys.mpaas", "com.xdap.*" ,"com.xdap.*"})
public class MobilecardApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MobilecardApplication.class, args);
    }
 
}

Write the interface here to simulate the external interface for feign to call the external interface.

Define the controller

@Autowired
PrintService printService;

@PostMapping("/outSide")
public String test(@RequestBody TestDto testDto) {
    return printService.print(testDto);
}

define service

@Service
public interface PrintService {
    public String print(TestDto testDto);
}

define-serviceImpl

public class PrintServiceImpl implements PrintService {
 
    @Override
    public String print(TestDto testDto) {
        return "模拟外部系统的接口功能"+testDto.getId();
    }
}

Build Feigin's Service

define service

//此处name需要设置不为空,url需要在.properties中设置
@Service
@FeignClient(url = "${outSide.url}", name = "service2")
public interface FeignService2 {
    @RequestMapping(value = "/custom/outSide", method = RequestMethod.POST)
    @ResponseBody
    public String getMessage(@Valid @RequestBody TestDto testDto);
}

Define the controller

@Autowired
FeignService2 feignService2;
//测试feign调用外部接口入口
@PostMapping("/test2")
public String test2(@RequestBody TestDto testDto) {
    return feignService2.getMessage(testDto);
}

postman test

Because I use the project here, I need to add certain request header information, and the addition of Feign request headers will be added later.

Add the following:

Add Header solution

Put token and other information into the Feign request header, mainly through the rewritten RequestInterceptorapply method

define config

@Configuration
public class FeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //添加token
        requestTemplate.header("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ4ZGFwYXBwaWQiOiIzNDgxMjU4ODk2OTI2OTY1NzYiLCJleHAiOjE2NjEyMjY5MDgsImlhdCI6MTY2MTIxOTcwOCwieGRhcHRlbmFudGlkIjoiMzAwOTgxNjA1MTE0MDUyNjA5IiwieGRhcHVzZXJpZCI6IjEwMDM0NzY2MzU4MzM1OTc5NTIwMCJ9.fZAO4kJSv2rSH0RBiL1zghdko8Npmu_9ufo6Wex_TI2q9gsiLp7XaW7U9Cu7uewEOaX4DTdpbFmMPvLUtcj_sQ");
    }
}

define service

@Service
@FeignClient(url = "${outSide.url}",name = "feignServer", configuration = FeignDemoConfig.class)
public interface TokenDemoClient {
    @RequestMapping(value = "/custom/outSideAddToken", method = RequestMethod.POST)
    @ResponseBody
    public String getMessage(@Valid @RequestBody TestDto testDto);
}

Define the controller

//测试feign调用外部接口入口,加上token
@PostMapping("/testToken")
public String test4(@RequestBody TestDto testDto) {
    return tokenDemoClient.getMessage(testDto);
}

Copyright statement: This article is an original article by CSDN blogger "Chelsea", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting. Original link: https://blog.csdn.net/Chelsea/article/details/126689495

 

Guess you like

Origin blog.csdn.net/qq_45635347/article/details/131460652