|  | 
 
| springboot使用resttemplate,传递多个参数 在 Spring Boot 中使用  `RestTemplate`  传递多个参数(如  `String md5` ,  `String ext` ,  `byte[] fileBytes` )时,可以通过构建一个请求体对象来实现。以下是一个示例,展示如何使用  `RestTemplate`  发送 POST 请求并传递这些参数。
 ### 1. 创建请求体类
 首先,我们需要创建一个请求体类来封装要发送的参数:
 
 复制代码public class FileUploadRequest {
    private String md5;
    private String ext;
    private byte[] fileBytes;
     public FileUploadRequest(String md5, String ext, byte[] fileBytes) {
        this.md5 = md5;
        this.ext = ext;
        this.fileBytes = fileBytes;
    }
     // Getter 和 Setter 方法
    public String getMd5() {
        return md5;
    }
     public void setMd5(String md5) {
        this.md5 = md5;
    }
     public String getExt() {
        return ext;
    }
     public void setExt(String ext) {
        this.ext = ext;
    }
     public byte[] getFileBytes() {
        return fileBytes;
    }
     public void setFileBytes(byte[] fileBytes) {
        this.fileBytes = fileBytes;
    }
}
 ### 2. 使用  `RestTemplate`  发送请求
 接下来,在你的控制器中使用  `RestTemplate`  发送 POST 请求,并将请求体传递到 B 服务器。
 
 复制代码import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 @RestController
public class FileUploadController {
     @Autowired
    private RestTemplate restTemplate;
     @PostMapping("/uploadFile")
    public String uploadFile(String md5, String ext, byte[] fileBytes) {
        String bServerUrl = "http://B_SERVER_URL/upload"; // 替换为 B 服务器的实际 URL
         // 创建请求体对象
        FileUploadRequest request = new FileUploadRequest(md5, ext, fileBytes);
         // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
         // 创建请求实体
        HttpEntity<FileUploadRequest> requestEntity = new HttpEntity<>(request, headers);
         // 发送 POST 请求
        String response = restTemplate.postForObject(bServerUrl, requestEntity, String.class);
         return response; // 返回 B 服务器的响应
    }
}
 ### 3. 配置  `RestTemplate`
 确保在你的 Spring Boot 应用中配置了  `RestTemplate`  的 bean:
 
 复制代码import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
 @Configuration
public class AppConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
 ### 总结
 1. 创建一个请求体类  `FileUploadRequest`  来封装要传递的参数。
 2. 在控制器中使用  `RestTemplate`  发送 POST 请求,并将请求体对象作为参数发送到 B 服务器。
 3. 确保配置了  `RestTemplate`  的 bean。
 通过这种方式,你可以方便地将多个参数传递给 B 服务器。
 
 
 
  
 
 可以使用 Lombok 来简化  `FileUploadRequest`  类的代码。Lombok 可以自动生成 getter、setter、构造函数等,减少样板代码。以下是如何使用 Lombok 改进  `FileUploadRequest`  类的示例。
 ### 1. 添加 Lombok 依赖
 首先,确保在你的  `pom.xml`  文件中添加了 Lombok 的依赖(如果你使用 Maven):
 
 复制代码xml
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version> <!-- 请检查最新版本 -->
    <scope>provided</scope>
</dependency>
 ### 2. 使用 Lombok 改进  `FileUploadRequest`  类
 接下来,我们可以使用 Lombok 的注解来简化  `FileUploadRequest`  类:
 
 复制代码import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 @Data
@NoArgsConstructor
@AllArgsConstructor
public class FileUploadRequest {
    private String md5;
    private String ext;
    private byte[] fileBytes;
}
 ### 3. 更新控制器
 控制器的代码可以保持不变,仍然使用  `FileUploadRequest`  类:
 
 复制代码import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 @RestController
public class FileUploadController {
    @Autowired
    private RestTemplate restTemplate;
     @PostMapping("/uploadFile")
    public String uploadFile(@RequestBody FileUploadRequest request) {
        String bServerUrl = "http://B_SERVER_URL/upload"; // 替换为 B 服务器的实际 URL
         // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
         // 创建请求实体
        HttpEntity<FileUploadRequest> requestEntity = new HttpEntity<>(request, headers);
         // 发送 POST 请求
        String response = restTemplate.postForObject(bServerUrl, requestEntity, String.class);
        return response; // 返回 B 服务器的响应
    }
}
 ### 总结
 1. 使用 Lombok 的  `@Data`  注解自动生成 getter 和 setter。
 2. 使用  `@NoArgsConstructor`  和  `@AllArgsConstructor`  来生成无参构造函数和全参构造函数。
 3. 控制器代码保持不变,仍然使用  `FileUploadRequest`  类。
 通过使用 Lombok,可以显著减少样板代码,使代码更加简洁。
 
 
 | 
 |