使用Jackson的ObjectMapper将映射转换为POJO会引发异常

问题描述 投票:-1回答:1

我有一个MultivaluedMap<String, String>,它代表POST请求的形式参数。我只想使用需要进一步处理的那些字段从此类转换POJO。我从杰克逊convertValue()中找到了一些建议使用ObjectMapper的答案。

Convert a Map<String, String> to a POJO

public void process(MultivaluedMap<String, String> formParams) {
  ObjectMapper objectMapper = new ObjectMapper();
  final MyPojo myPojo = objectMapper.convertValue(formParams,MyPojo.class);
}

POJO

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyPojo {

    private String status;

    @JsonProperty("order_no")
    private String orderId;

    @JsonProperty("tid")
    private String transactionId;
}

但是,此操作失败,但以下例外:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: MyPojo["order_no"])

这是调试器中输入的样子:

Debugger screenshot

jackson objectmapper
1个回答
0
投票

我需要用似乎有效的String替换ArrayList<String>类型:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)

public class MyPojo {

    private ArrayList<String> status;

    @JsonProperty("order_no")
    private ArrayList<String> orderId;

    @JsonProperty("tid")
    private ArrayList<String> transactionId;
}
© www.soinside.com 2019 - 2024. All rights reserved.