我将 JAX-RS (CXF) 与 JaxB 和 Jackson 一起使用来提供 REST-API。不幸的是,找到的结果都没有帮助我解决以下(简单)问题:
我实现了以下方法:
@POST
@Path(ApiStatics.ARMY_CREATE_ARMY)
public com.empires.web.dto.Army createArmy(@FormParam("locationid") long locationId, @FormParam("name") String name, @FormParam("troops") ArmyTroops troops) {
这是我的模型类:
@XmlRootElement
@XmlSeeAlso(ArmyTroop.class)
public class ArmyTroops {
public ArmyTroops() {
}
public ArmyTroops(List<ArmyTroop> troops) {
this.troops = troops;
}
@XmlElement(name = "troops")
private List<ArmyTroop> troops = new ArrayList<ArmyTroop>();
public List<ArmyTroop> getTroops() {
return troops;
}
public void setTroops(List<ArmyTroop> troops) {
this.troops = troops;
}
}
陆军部队
@XmlRootElement(name = "troops")
public class ArmyTroop {
@XmlElement
private long troopId;
@XmlElement
private String amount;
public long getTroopId() {
return troopId;
}
public void setTroopId(long troopId) {
this.troopId = troopId;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
我发送的 json 如下所示:
locationid 1
name asdasd
troops {"troops":[{"troopId":4,"amount":"5"},{"troopId":6,"amount":"5"}]}
不幸的是,对象没有被转换。相反,我收到此错误:
InjectionUtils #reportServerError - Parameter Class com.empires.web.dto.in.ArmyTroops has no constructor with single String parameter, static valueOf(String) or fromString(String) methods
如果我为构造函数提供一个字符串参数,我会得到传递给“部队”的整个 json 字符串,如上所述。
任何想法为什么 JaxB 在这一点上不起作用?