我正在使用JAX (CXF)与JaxB和杰克逊提供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;
}
}ArmyTroop
@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在这一点上不起作用吗?
发布于 2013-07-06 08:53:31
您正在使用@Form注释传递所有参数。但是http消息的表单部分必须是xml数据结构。您的3个参数没有主xml数据结构,因此无法工作。简而言之,表单对是作为身体发送的。Cxf使用MultivaluedMap发送params (cxf有此结构的xml模型)。正如您所看到的,它不适合那些不能进行琐碎序列化的参数。
在这里,我的解决方案是放弃@FormParam以避免这个问题:
1)使用@PathParam @CookieParam发送您的前2个参数,而“no标记”(body)仅用于陆军组合。
2)定义一个接受所有参数并可以序列化为xml数据结构并使用'no tag‘(body)发送的uber对象。
3)使用soap,使用cxf可以轻松地获得Rest和Soap。
https://stackoverflow.com/questions/17496651
复制相似问题