
在 Spring Boot 开发过程中,我们常常需要处理前端传递过来的 JSON 数据。通常,我们会使用一个对象来接收这些数据,但有时我们可能会遇到一些特殊需求,比如将一个 JSON 字符串拆分到两个对象中。本文将探讨如何在 Spring Boot 中实现这一需求,并给出详细的代码示例和解释。

假设我们有这样一个 JSON 字符串:
{
"name": "John",
"age": 30,
"address": {
"street": "123 Main St",
"city": "Springfield"
}
}我们希望将这个 JSON 字符串中的 name 和 age 字段映射到一个对象 Person 中,而 address 字段映射到另一个对象 Address 中。这种需求在处理复杂的数据结构时并不少见。
要实现这个功能,我们可以利用 Spring Boot 的 @RequestBody 注解和自定义的转换器。具体步骤如下:
Person 和 Address。PersonWithAddress。PersonWithAddress 对象。@RequestBody 注解接收 JSON 数据。Person 和 Address 类首先,我们定义 Person 和 Address 类:
public class Person {
private String name;
private int age;
// Getters and Setters
}public class Address {
private String street;
private String city;
// Getters and Setters
}PersonWithAddress 类接下来,我们定义一个包含 Person 和 Address 对象的类:
public class PersonWithAddress {
private Person person;
private Address address;
// Getters and Setters
}我们需要创建一个自定义转换器,将 JSON 数据转换为 PersonWithAddress 对象。这里我们使用 Jackson 的 ObjectMapper 进行转换:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class PersonWithAddressConverter extends AbstractJackson2HttpMessageConverter {
public PersonWithAddressConverter(ObjectMapper objectMapper) {
super(objectMapper);
}
@Override
protected boolean canRead(MediaType mediaType) {
return true;
}
@Override
protected boolean supports(Class<?> clazz) {
return PersonWithAddress.class.equals(clazz);
}
@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(inputMessage.getBody());
Person person = new Person();
person.setName(rootNode.get("name").asText());
person.setAge(rootNode.get("age").asInt());
Address address = new Address();
address.setStreet(rootNode.get("address").get("street").asText());
address.setCity(rootNode.get("address").get("city").asText());
PersonWithAddress personWithAddress = new PersonWithAddress();
personWithAddress.setPerson(person);
personWithAddress.setAddress(address);
return personWithAddress;
}
}在 Spring Boot 中配置自定义的转换器:
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final PersonWithAddressConverter personWithAddressConverter;
public WebConfig(PersonWithAddressConverter personWithAddressConverter) {
this.personWithAddressConverter = personWithAddressConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(personWithAddressConverter);
}
}@RequestBody 接收 JSON 数据在控制器中接收 JSON 数据并处理:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
@PostMapping("/person")
public String createPerson(@RequestBody PersonWithAddress personWithAddress) {
Person person = personWithAddress.getPerson();
Address address = personWithAddress.getAddress();
return "Received person: " + person.getName() + ", age: " + person.getAge() +
" with address: " + address.getStreet() + ", " + address.getCity();
}
}通过以上步骤,我们成功实现了将一个 JSON 字符串拆分到两个对象中的需求。在实际应用中,这种方法可以帮助我们更灵活地处理复杂的数据结构,使代码更加清晰和可维护。
这种方法不仅仅适用于简单的对象拆分,还可以扩展到更复杂的场景中。希望本文对你理解和实践 Spring Boot 中的 JSON 数据处理有所帮助。