我有一个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);
}
@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"])
这是调试器中输入的样子:
我需要用似乎有效的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;
}