Spring Boot:JSON解析错误:无法构造ウcom.example.new_model.BenzenoidCriterions Ж的示例,

g52tjvyc  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(134)

我正在使用SpringBoot,并且我有以下控制器:

@Autowired
BenzenoidService service;

@PostMapping("/find_benzenoid")
public List<Benzenoid> find(@RequestBody BenzenoidCriterions benzenoidData) {
    Triplet<String[], Operator[], Object[]> arrays = buildArrays(benzenoidData);
    return service.findBenzenoids(arrays.getX(), arrays.getY(), arrays.getZ());
}

以及以下@请求机构苯标准:

public class BenzenoidCriterions {

    private String id = "";
    private String name = "";
    private String nbHexagons = "";
    private String nbCarbons = "";
    private String nbHydrogens = "";
    private String irregularity = "";

    public BenzenoidCriterions() {
        super();
    }

    public BenzenoidCriterions(String id, String name, String nbHexagons,
            String nbCarbons, String nbHydrogens, String irregularity) {
        this.id = id;
        this.name = name;
        this.nbHexagons = nbHexagons;
        this.nbCarbons = nbCarbons;
        this.nbHydrogens = nbHydrogens;
        this.irregularity = irregularity;
    }

    // getters
}

实际上,当我提供一个包含BenzenoidCriterions所有字段的JSON输入时,它是有效的,但是当我跳过输入中的一个或多个字段时,我得到了以下错误消息:

2022-10-25 13:53:37.409  WARN 25509 --- [nio-8894-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.example.new_model.BenzenoidCriterions`, problem: `java.lang.NullPointerException`; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `com.example.new_model.BenzenoidCriterions`, problem: `java.lang.NullPointerException`
 at [Source: (PushbackInputStream); line: 1, column: 91]]

这个问题似乎与Jackson有关,Jackson应该解析JSON输入并将其转换为BenzenoidCriterions的示例。
有人能解决这个问题吗?

  • 谢谢-谢谢
ltskdhd1

ltskdhd11#

Jackson提供了一个可以在类级别上使用的注解,如下所示

@JsonIgnoreProperties(ignoreUnknown = true)
public class BenzenoidCriterions
{
    ...
}

这样Jackson将忽略缺少的字段。

相关问题