Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RestTemplate.exchange创建实例,即使变量注释为@NotNull_Java_Spring_Spring Mvc_Spring Rest - Fatal编程技术网

Java RestTemplate.exchange创建实例,即使变量注释为@NotNull

Java RestTemplate.exchange创建实例,即使变量注释为@NotNull,java,spring,spring-mvc,spring-rest,Java,Spring,Spring Mvc,Spring Rest,所以我有一些课程。例: public class Operator { @NotNull private Integer id; @NotNull private String operatorName; } 我想用RestTemplate从另一个微服务获取这个对象的映射 @Bean public Map<String, Operator> operatorsMap(RestTemplate restTemplate, UriFactory ur

所以我有一些课程。例:

public class Operator {

    @NotNull
    private Integer id;

    @NotNull
    private String operatorName;
}
我想用RestTemplate从另一个微服务获取这个对象的映射

@Bean
public Map<String, Operator> operatorsMap(RestTemplate restTemplate, UriFactory uriFactory) {

    Map<String, Operator> map = Objects.requireNonNull(restTemplate.exchange(uriFactory.getOperatorsURI(),
            HttpMethod.GET,
            null,
            new ParameterizedTypeReference<Map<String, Operator>>() {
            }).getBody());
    LOG.info("Successfully loaded data for {} operators", map.size());
    return Collections.unmodifiableMap(map);
}
@Bean
公共映射运算符映射(RestTemplate RestTemplate,UriFactory UriFactory){
Map Map=Objects.requireNonNull(restTemplate.exchange(uriFactory.getOperatorUri()),则,
HttpMethod.GET,
无效的
新的ParameteredTypeReference(){
}).getBody());
LOG.info(“为{}运算符成功加载数据”,map.size());
返回集合。不可修改映射(map);
}

问题是-即使id或operatorName从externalService附带null,此restTemplate也会创建operator对象并将此字段设置为null。我怎样才能防止这种行为。对我来说,理想的行为是异常和不启动应用程序。

我认为这是因为在反序列化过程中需要强制执行底层json对象映射器

public class Operator {

    @JsonProperty(required = true)
    @Required
    @NotNull
    private Integer id;

    @JsonProperty(required = true)
    @Required
    @NotNull
    private String operatorName;

    Operator(@NotNull id, @NotNull operatorName) {
      this.id = id;
      this.operatorName = operatorName;
    }

}

字段中仍然为空:-(您可以尝试切换到
@Nonnull
注释吗?Thx以获得帮助。但字段仍然为空。我可以通过执行
公共无效setOperatorName(字符串operatorName)引发异常{if(operatorName==null){throw new Exception();}来避免此问题this.operatorName=operatorName;}
但它不是很满