如何从嵌套的XML子元素中删除命名空间值?

时间:2013-05-15 19:57:49

标签: java xml web-services java-ee serialization

我有一个由Jackson,Jersey,EclipseLink和Tomcat提供支持的小型RESTful Web服务。在大多数情况下,除了涉及嵌套子元素之外,XML正在按预期工作。当XML文档的其余部分不是时,它们使用命名空间属性编写。以下是响应XML的示例:

<userView>
    <userIdSID>5</userIdSID>
    <userAuthLevel>5</userAuthLevel>
    <userRoles>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">1</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">3</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">2</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">8</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">5</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">4</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">9</roleSID>
        <roleSID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:decimal">1000001</roleSID>
    </userRoles>
</userView>

这就是我的UserRoleList.java:

package com.company.project;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD)
public class UserRoleList implements Serializable {

    private int userRoleSID;

    public int getUserRoleSid() {
        return userRoleSID;
    }

    public void setUserRoleSid(int userRoleSid) {
        this.userRoleSID = userRoleSid;
    }
}

这就是我的UserView.java:

package com.company.project;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class UserView implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "USER_ID_SID")
    private String userIdSID;
    @Basic(optional = true)
    private String userAuthLevel;
    @XmlElementWrapper(name = "userRoles")
    @XmlElement(name = "roleSID")
    private List<UserRoleList> userRoleList = new ArrayList<UserRoleList>();

    public UserView() {
    }

    public UserView(String userIdSID) {
        this.userIdSID = userIdSID;
    }

    public String getUserIdSID() {
        return userIdSID;
    }

    public void setUserIdSID(String userIdSID) {
        this.userIdSID = userIdSID;
    }

    public List<UserRoleList> getUserRoleList() {
        return userRoleList;
    }

    public void setUserRoleList(List<UserRoleList> userRoleList) {
        this.userRoleList = userRoleList;
    }

    public String getUserAuthLevel() {
        return userAuthLevel;
    }

    public void setUserAuthLevel(String userAuthLevel) {
        this.userAuthLevel = userAuthLevel;
    }
}

这是我填充nest元素的方式,它需要2个入站参数并通过Entity Manager执行查询:

 List<UserRoleList> userRoleList = em.createNamedQuery("findRoleListByLoginName")
                    .setParameter(1, id)
                    .setParameter(2, client)
                    .getResultList();

关于我可能遗失某些东西的任何想法?我尝试过使用pkg-info无济于事。请注意,此输出构造的XML没有从中生成的模式。不确定这是否有帮助或阻碍,但是非常感谢任何帮助。

更新1 :根据要求,这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>moxyWS</display-name>
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <description>Multiple packages, separated by semicolon(;), can be specified in param-value</description>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.project.moxyws</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/webresources/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <persistence-unit-ref>
        <persistence-unit-ref-name>persistence-factory</persistence-unit-ref-name>
        <persistence-unit-name>moxyWS_war_1.0-SNAPSHOTPU</persistence-unit-name>
    </persistence-unit-ref>
</web-app>

更新2 :根据要求,这是我的REST外观:

package com.project.moxyws.service;

import com.project.moxyws.UserView;
import com.project.moxyws.controller.UserViewJPAController;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;

@Path("request")
public class UserViewRESTFacade {

    private EntityManagerFactory getEntityManagerFactory() throws NamingException {
        return (EntityManagerFactory) Persistence
                .createEntityManagerFactory("moxyWS_war_1.0-SNAPSHOTPU");
    }

    private UserViewJPAController getJpaController() {
        try {
            return new UserViewJPAController(getEntityManagerFactory());
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }

    public UserViewRESTFacade() {
    }

    @GET
    @Path("{client}/{id}")
    @Produces({"application/xml", "application/json"})
    public UserView findUserByClientAndID(@PathParam("client") String client, @PathParam("id") String id) {
        try {
            return getJpaController().findUserId(id, client);
        } catch (Exception e) {
            throw new WebApplicationException(204);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我认为问题是你的UserRoleList类,它是一个具有单个字段的类,你真的想要映射到一个简单类型。您可能想尝试使用userRoleSID注释注释@XmlValue成员。

答案 1 :(得分:1)

使用JAXB映射JPA实体时,我建议使用默认访问(或XmlAccessType.PROPERTY)并注释get方法。 JPA impls有一些技巧来处理延迟加载这样的事情,当使用字段访问时有时会导致问题。

答案 2 :(得分:0)

我无法确定一个答案,但他们全部共同给了我开始检查我的SQL onstructs的想法。事实证明,我的UserRoleList班级正在采用int的数据类型。在围栏的另一侧,我的ResultListBigDecimal数据类型的列表。

为了消除任何歧义,我修改了我的SQL语句,将值返回为VARCHAR,将其作为String数据类型对象。我更新了我的UserRoleList类来代表String个对象,现在一切正常。

我将重新审视我的代码以反映数据类型的正确绑定,但是现在,这让我超越了驼峰,所以我可以完成我的解决方案的原型设计。非常感谢所有人,尤其是 DMoses ,以便快速高效地进行头脑风暴会议。