gpt4 book ai didi

java - Spring Boot 预处理 JSON 数据

转载 作者:搜寻专家 更新时间:2023-11-01 02:37:43 25 4
gpt4 key购买 nike

我有一个 RESTful Spring Boot API,它有一个注册端点。

在我的 @RestController 类中,我编写了一个简单的字符串值预处理器,用于用 null 值修剪和替换只有空白的字符串。

  @InitBinder
public void blankStringBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("password", "confirmPassword");
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}

但是当我以原始 JSON 的形式从 Postman 提交数据时,修整编辑没有发生。我在 blankStringBinder 方法中放置了一个断点,我发现它会在每个传入请求上被调用。

WebDataBinder 似乎适用于 form-data。有没有办法让它也适用于原始 JSON 数据?

最佳答案

如果您只想对少数 String 字段执行此操作,请首先创建自定义 JsonDeserializer:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

public class EmptyToNullCustomDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
JsonNode node = jp.readValueAsTree();
if (node.textValue().isEmpty()) {
return null;
}
return node.textValue();
}
}

然后在 POJO 的每个 String 上添加此注释:@JsonDeserialize(using = EmptyToNullCustomDeserializer.class)。例如:

@JsonDeserialize(using = EmptyToNullCustomDeserializer.class)
private String content;

编辑:

如果您有很多要预处理的 String 字段,那么对每个字段进行注释会非常麻烦。

作为替代方案,您可以对所有 String 字段进行预处理,而根本不必对它们进行注释。为此,您必须首先修改 EmptyToNullCustomDeserializer,使其父类为 Jacksons StdDeserializer:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import org.springframework.stereotype.Component;

@Component
public class EmptyToNullCustomDeserializer extends StdDeserializer<String> {
protected EmptyToNullCustomDeserializer() {
super(String.class);
}

@Override
public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
JsonNode node = jp.readValueAsTree();
if (node.textValue().isEmpty()) {
return null;
}
return node.textValue();
}
}

然后通过将上述解串器添加到配置中来创建此组件以自定义 Jackson 对象映射器:

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Component;

@Component
public class JacksonConfiguration {

@Autowired
private EmptyToNullCustomDeserializer emptyToNullCustomDeserializer;

@Primary
@Bean
public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder(List<Jackson2ObjectMapperBuilderCustomizer> customizers) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
for (Jackson2ObjectMapperBuilderCustomizer customizer : customizers) {
customizer.customize(builder);
}
return builder;
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer addEmptyToNullStringDeserialization() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.deserializerByType(String.class, emptyToNullCustomDeserializer);
}

};
}
}

关于java - Spring Boot 预处理 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001173/

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