我正在寻找一种基于用户提供的xml模板转换XML的方法。 我有一个springboot服务正在生成输出, 这是一个基本示例,但该订单项的“产品”标签下还有其他嵌套元素,还包含收费税...
OrderHeader
package com.example.microservice.dao;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonProperty;
@Entity
@Table(name="ORDER_HEADER")
public class OrderHeader {
@Id
@Column(name="ORDER_HEADER_KEY", nullable=false)
private String orderHeaderKey;
@Column(name="ORDER_NO", nullable=false)
private String orderNo;
@Column(name="ENTERPRISE_KEY", nullable=false)
private String enterpriseKey;
@Column(name="SHIP_TO_KEY", nullable=false)
private String shipToKey;
@Column(name="PAYMENT", nullable=false)
private String payment;
@Column(name="AUTHORISED", nullable=false)
private String authorised;
@Column(name="CUSTOMER", nullable=false)
private String customer;
@OneToMany(mappedBy="oHeader")
private List<OrderLine> orderlines;
public OrderHeader() {
super();
// TODO Auto-generated constructor stub
}
public OrderHeader(String orderHeaderKey, String orderNo, String enterpriseKey, String shipToKey) {
super();
this.orderHeaderKey = orderHeaderKey;
this.orderNo = orderNo;
this.enterpriseKey = enterpriseKey;
this.shipToKey = shipToKey;
}
// Getters & Setters
//To String method override
}
OrderLine 实体类
package com.example.microservice.dao;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="ORDER_LINE")
public class OrderLine {
@Id
@Column(name="ORDER_LINE_KEY")
private String orderLineKey;
@Column(name="ORDER_HEADER_KEY")
private String orderHeaderKey;
@Column(name="ITEM_ID")
private String itemID;
@Column(name="PRIME_LINE_NO")
private String primeLineNo;
@Column(name="ORDERED_QTY")
private String orderedQty;
@Column(name="LINE_TOTAL")
private String lineTotal;
@ManyToOne
@JoinColumn(name = "ORDER_HEADER_KEY",referencedColumnName="ORDER_HEADER_KEY", insertable=false, updatable=false)
private OrderHeader oHeader;
// Geters & Setters
// To String override
}
MicroService Controller方法
@RestController
public class MicroServiceController {
@Autowired
OrderHeaderRepository orderHeaderRepo;
@Autowired
OrderLineRepository orderLineRepo;
public MicroServiceController() {
// TODO Auto-generated constructor stub
}
@RequestMapping(path="/order/{orderID}",
method= {RequestMethod.POST})
public List<OrderHeader> orderInfoID(@PathVariable(name="orderID") String orderID, @RequestBody String userPreferedTemplate) {
System.out.println("API output template from user----->"+userPreferedTemplate);
List<OrderHeader> oH = orderHeaderRepo.findByOrderHeaderKey(orderID);
System.out.println("JPA Query output for order entity \n"+oH.toString());
return oH;
}
}
对以上代码的响应如下,它按顺序和产品级别返回整个属性。
<Order OrderNumber="223344" Customer="Bob" Payment="CreditCard" Authorised="Y">
<Product Line="1" ProductId="Item_1001" Quantity="2.00" LineTotal="200" />
<Product Line="2" ProductId="Item_1002" Quantity="2.00" LineTotal="400" />
<Product Line="3" ProductId="Item_1003" Quantity="2.00" LineTotal="600" />
</Order>
POSTMAN_REQ_IMAGE
用户请求的属性作为回报
<Order OrderNumber="" >
<Product Line="" ProductId="" />
</Order>
<Order OrderNumber="223344" >
<Product Line="1" ProductId="Item_1001" />
<Product Line="2" ProductId="Item_1002" />
<Product Line="3" ProductId="Item_1003" />
<Product Line="4" ProductId="Item_1004" />
</Order>
**如果用户在模板中传递了无效标签
<Order OrderNumber="" >
<ProductLINESSSS Line="" ProductId="" />
</Order>
然后将忽略此类标签,并且输出应类似于
<Order OrderNumber="223344" >
</Order>
之所以这样做是因为作为一个实体的订单与产品,送货地址,账单地址等,付款有关系... 对于想要使用订单号检索客户信息的用户,产品,地址,付款的其余信息无关。 对于希望检索产品的应用程序,可以相应地提供模板。
也
我不确定JPA是否可以实现此目标,或者xslt转换可以做到这一点,但会感谢您输入正确的方法来实现这一目标。