在Scala中,我正在尝试从Java POJO类转换为Scala Case类。请在下面找到详细信息:
//Java POJO Response Class
@JsonIgnoreProperties(ignoreUnknown = true)
public class IncidentResponse {
private String issueType;
private List<OrderDetailResponse> orderDetails;
//GETTER and SETTERs}
@JsonIgnoreProperties(ignoreUnknown = true)
public class OrderDetailResponse {
private String orderId;
private String orderItemId;
//GETTER and SETTERs
public List<OrderDetailResponse> getOrderDetails() {return this.orderDetails;}}
//Scala Case Class
@JsonNaming(classOf[SnakeCaseStrategy])
@JsonIgnoreProperties(ignoreUnknown = true)
case class IncidentDetails(issueType: String,
orderDetails: List[OrderDetailResponse])
//OrderDetailResponse is from the Scala Case Class
@JsonNaming(classOf[SnakeCaseStrategy])
@JsonIgnoreProperties(ignoreUnknown = true)
case class OrderDetailResponse(orderId:String,
orderItemId:String)
我已经尝试了以下方法来转换SnakeCase中的数据 -
变量“data”包含JAVA POJO Class的IncidentResponse
val orderDetails = data.getOrderDetails.asInstanceOf[List[Map[String, Any]]].map(p => (mapper.convertValue(p, typeReference[OrderDetailResponse](tTag)).asInstanceOf[OrderDetailResponse].get))
IncidentDetails(data.getIssueType,orderDetails)
得到错误 - java.util.ArrayList无法强制转换为scala.collection.immutable.List
尝试转换List [Map [Object,Object]]:
val scalaList =data.getOrderDetails.asScala.toList.asInstanceOf[List[Map[Object,Object]]]
IncidentDetails(data.getIssueType,
scalaList.asInstanceOf[List[OrderDetailResponse]])
没有得到任何错误,但它没有转换为OrderDetailResponse的SnakeCase但是issType被转换为SnakeCase
如果我从POJO类中单独分配值,那么它将起作用,如果我有许多字段,那么它将导致许多代码行,因此如何从Java POJO类转换为Scala案例类的SnakeCase。 / p>
IncidentDetails(data.getIssueType,
data.getOrderDetails.asScala.head.map(orderResp =>
OrderDetailResponse(orderResp.getOrderId,orderResp.getOrderItemId)
)