Spring RestTemplate发送空字符串会被视为null吗?

huangapple go评论70阅读模式
英文:

Spring RestTemplate sends empty string as null?

问题

我正在共同开发一段代码,它通过POST方式从外部微服务获取一些数据,使用JSON格式进行双向通信。问题在于,我发送了一个空字符串,但每当我发送一个空字符串时,微服务都会返回一个错误消息,建议我发送的字段为空。添加一个空格字符可以解决这个问题。是否有可能序列化出现了问题?

谢谢提前。

这是我的RestTemplate:

final HttpEntity<?> httpRequestEntity = new HttpEntity<>(employeeRequest, copyAndAdjustHeaders(httpHeaders));
final ResponseEntity<EmployeeResponseDto> exchange = restTemplate.exchange(employeeResourcePath, HttpMethod.POST, httpRequestEntity, EmployeeResponseDto.class);
final EmployeeResponseDto body = exchange.getBody();

我的对象映射器配置:

@Bean
public ObjectMapper objectMapper() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
    objectMapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
    return objectMapper;
}

以及我在构造函数中初始化@Autowired RestTemplate的方式:

this.restTemplate = restTemplateBuilder
        .setConnectTimeout(requestConnectionTimeoutMillis)
        .setReadTimeout(requestReadTimeoutMillis)
        .rootUri(employeeRootUri)
        .build();

希望这些信息对你有所帮助。

英文:

I'm co-developing a piece of code that fetches some data via POST from an external microservice using JSON format both ways. The issue is that I'm sending an empty String, but the microservice comes back to me with an error message suggesting the fields I send are null whenever I send an empty String. Adding a space character eliminates the issue. Is there any chance that somehow the serialization is going wrong?

Thank you in advance.

This is my rest template:

final HttpEntity&lt;?&gt; httpRequestEntity = new HttpEntity&lt;&gt;(employeeRequest, copyAndAdjustHeaders(httpHeaders));
            final ResponseEntity&lt;EmployeeResponseDto&gt; exchange = restTemplate.exchange(employeeResourcePath, HttpMethod.POST, httpRequestEntity, EmployeeResponseDto.class);
            final EmployeeResponseDto body = exchange.getBody();

My object mapper configuration:

    @Bean
    public ObjectMapper objectMapper() {
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
        objectMapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
        return objectMapper;
    }

And how i initialize the autowired RestTemplate in the constructor.

        this.restTemplate = restTemplateBuilder
                .setConnectTimeout(requestConnectionTimeoutMillis)
                .setReadTimeout(requestReadTimeoutMillis)
                .rootUri(employeeRootUri)
                .build();

答案1

得分: 3

在您的映射器上禁用ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,使用以下代码:

objectMapper.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

ACCEPT_EMPTY_STRING_AS_NULL_OBJECT: 可以启用的功能,允许将JSON中的空字符串值("")绑定到POJOs作为null。

英文:

Disable the ACCEPT_EMPTY_STRING_AS_NULL_OBJECT on your mapper with:

objectMapper.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

> ACCEPT_EMPTY_STRING_AS_NULL_OBJECT: Feature that can be enabled to allow JSON empty String value ("") to be bound to POJOs as null.

答案2

得分: 0

解决方法是在执行发布操作的微服务中使用@NotNull@JsonInclude(JsonInclude.Include.NON_NULL)来注解字段。尽管在不同环境中并不总是复制每一次,但现在它正常工作了。

英文:

The solution that solved it was to annotate the fields with @NotNull and @JsonInclude(JsonInclude.Include.NON_NULL) in the microservice that did the posting. It was still weird though it didn't replicate every single time across different environments. Now it works fine.

huangapple
  • 本文由 发表于 2020年8月10日 18:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63338042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定