What is Retrofit?

  • Take a look at Retrofit’s website

    square.github.io/retrofit/

    A type-safe HTTP client for Android and Java

    Retrofit is based on the network request framework encapsulated by OKHTTP. The underlying usage of the network request is actually completed using OKHTTP. Retrofit is only responsible for the encapsulation of the network request interface.

    Using Retrofit you can make HTTP call requests as elegant and silky as local calls.

Spring Boot uses Retrofit to quickly develop more elegant HTTP requests

  • Start with a new SpringBoot project, here is a recommended website to create a Java project:

    Start.aliyun.com/bootstrap.h…

    Check the desired configuration and select get code. You can download it directly or use Git to get it

  • Introduce retroFIT dependencies

    <dependency> <groupId>com.github.lianjiatech</groupId> <artifactId>retrofit-spring-boot-starter</artifactId> < version > 2.0.2 < / version > < / dependency > < the dependency > < groupId > com. Squareup. Retrofit2 < / groupId > < artifactId > converter - gson < / artifactId > < version > 2.0.2 < / version > < / dependency >Copy the code
  • Write the test request interface

       /** * Retrofit calls interface **@return* /
        @GetMapping("/retrofit")
        @ResponseBody
        public String retrofit(a) {
            return "retrofit";
        }
    
    Copy the code
  • Write the RetroFIT interface

    package com.example.demo.api;
    
    import okhttp3.ResponseBody;
    import retrofit2.Call;
    import retrofit2.http.GET;
    
    / * * *@Description:
     * @Author: yezi
     * @Date: 2021/3/12 15:00 * /
    public interface DemoApiService {
    
    
        @GET("/retrofit")
        Call<ResponseBody> demoHttp(a);
    
    }
    
    Copy the code
  • Write retroFIT configuration classes

    package com.example.demo.api;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    
    / * * *@Description:
     * @Author: yezi
     * @Date: 2021/3/12 o * /
    @Configuration
    public class ApiConfig {
    
        @Bean
        public DemoApiService demoApiService(a){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://127.0.0.1:8080")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            DemoApiService demoApiService = retrofit.create(DemoApiService.class);
            returndemoApiService; }}Copy the code
  • Writing test requests

    package com.example.demo;
    
    import com.example.demo.api.DemoApiService;
    import okhttp3.ResponseBody;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import retrofit2.Call;
    
    import java.io.IOException;
    
    @SpringBootTest
    class DemoApplicationTests {
    
        @Autowired
        DemoApiService demoApiService;
    
        @Test
        void contextLoads(a) throws IOException { Call<ResponseBody> responseBodyCall = demoApiService.demoHttp(); String string = responseBodyCall.execute().body().string(); System.out.println(string); }}Copy the code
  • The test results

Retrofit allows you to use retrofit to call HTTP requests as if you were calling local methods