如何使用CXF正确设置JAXB注释?

时间:2014-07-18 19:45:03

标签: json spring jaxb cxf jax-rs

我试图在我的rest api中获取一个@Post方法来正确解释它正在消耗的JSON。但是我一直收到这个错误:

WARNING: javax.xml.bind.UnmarshalException
- with linked exception:
[com.sun.istack.SAXParseException2; columnNumber: 0; unexpected element (uri:"",
local:"name"). Expected elements are <{}Dock>,<{}Door>,<{}Slot>,<{}Yard>,<{}Zone>]

我的服务类看起来像这样:

@Consumes("application/json")
@Produces("application/json")
@Path("/")
public class YardActions implements YardHandler{
    YardHandler yard;

    @PostConstruct
    public void init() {
        yard = new YardHandlerImpl();
    }

    //GET Actions
    @GET
    @Path("yards")
    public List<Yard> getYards() {
        //System.out.println(Uri);
        return yard.getYards();
    }

    @GET
    @Path("yard/{yardName}")
    public Yard getYard(@PathParam("yardName") String yardName){
        return yard.getYard(yardName);
    }

    //POST Actions
    @POST
    @Consumes("application/json")
    @Path("yard/")
    public void addYard(Yard newYard) {
        System.out.println(newYard);
        yard.addYard(newYard);
    }

    //PUT
    @PUT
    @Path("yard/{yardName}")
    public void updateYard(){
        yard.updateYard();
    }

    //DELETE
    @DELETE
    @Path("yard/{yardName}")
    public void deleteYard(String yardName) {
        yard.deleteYard(yardName);
    }
}

我的围场班模型看起来像这样:

@XmlRootElement(name = "Yard")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {})
public class Yard {
    //@XmlElement
    private String name;
    //@XmlElement
    private Location location;
    //@XmlElement
    private List<Zone> zones;
    //@XmlElement
    private List<Dock> docks;

    //Yard Name getter and setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    //Zone getter and setter
    public List<Zone> getZones() {
        return zones;
    }

    public void setZones(List<Zone> zones) {
        this.zones = zones;
    }

    public List<Dock> getDocks() {
        return docks;
    }

    public void setDocks(List<Dock> docks) {
        this.docks = docks;
    }
}

我发送的JSON看起来像这样

{
    "name" : "yard3",
    "location" : ""
}

我觉得我只是错过了某些配置。我绑错了我的班级吗?我需要加载另一个bean吗?我已经研究了几个小时了,任何帮助都表示赞赏:)

1 个答案:

答案 0 :(得分:0)

我的问题是库的错误。我使用的是CXF 2.7。升级到CXF 3.0后,事情正在按预期进行。