gpt4 book ai didi

json - 如何将 Multipart 文件和 json 数据发送到 Spring Boot

转载 作者:行者123 更新时间:2023-12-02 11:24:31 26 4
gpt4 key购买 nike

我有 POST 请求 api 调用来接受来自客户端( postman 或 java 客户端)的 json 正文请求参数和多部分文件。

我想在单个请求中同时传递 json 数据和多部分文件。

我已经编写了如下代码。

@RequestMapping(value = "/sendData", method = RequestMethod.POST, consumes = "multipart/form-data")
public ResponseEntity<MailResponse> sendMail(@RequestPart MailRequestWrapper request) throws IOException

但是,我无法使用 postman 休息客户端完成它。

我在服务器端使用 spring boot。

任何人都可以在这个问题上建议我。

提前致谢,

最佳答案

你猫用@RequestParamConverter对于 JSON 对象
简单的例子:

@SpringBootApplication
public class ExampleApplication {

public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}

@Data
public static class User {
private String name;
private String lastName;
}

@Component
public static class StringToUserConverter implements Converter<String, User> {

@Autowired
private ObjectMapper objectMapper;

@Override
@SneakyThrows
public User convert(String source) {
return objectMapper.readValue(source, User.class);
}
}

@RestController
public static class MyController {

@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file,
@RequestParam("user") User user) {
return user + "\n" + file.getOriginalFilename() + "\n" + file.getSize();
}

}

}

和 postman :
enter image description here

更新
Apache httpclient 4.5.6例子:

pom.xml 依赖:
    <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.6</version>
</dependency>

<!--dependency for IO utils-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

服务将在应用程序完全启动后运行,更改 File你的文件路径
@Service
public class ApacheHttpClientExample implements ApplicationRunner {

private final ObjectMapper mapper;

public ApacheHttpClientExample(ObjectMapper mapper) {
this.mapper = mapper;
}

@Override
public void run(ApplicationArguments args) {
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
File file = new File("yourFilePath/src/main/resources/foo.json");
HttpPost httpPost = new HttpPost("http://localhost:8080/upload");

ExampleApplication.User user = new ExampleApplication.User();
user.setName("foo");
user.setLastName("bar");
StringBody userBody = new StringBody(mapper.writeValueAsString(user), MULTIPART_FORM_DATA);
FileBody fileBody = new FileBody(file, DEFAULT_BINARY);

MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addPart("user", userBody);
entityBuilder.addPart("file", fileBody);
HttpEntity entity = entityBuilder.build();
httpPost.setEntity(entity);

HttpResponse response = client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();

// print response
System.out.println(IOUtils.toString(responseEntity.getContent(), UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}

}

控制台输出将如下所示:
ExampleApplication.User(name=foo, lastName=bar)
foo.json
41

关于json - 如何将 Multipart 文件和 json 数据发送到 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com