gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:22:56 24 4
gpt4 key购买 nike

我有一个由 Jackson、Jersey、EclipseLink 和 Tomcat 提供支持的小型 RESTful Web 服务。在大多数情况下,XML 都按预期工作,但涉及我的嵌套子元素时除外。它们是用 namespace 属性编写的,而 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;
}
}

这就是我填充该嵌套元素的方式,它需要 2 个入站参数并通过实体管理器执行查询:

 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 Facade:

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);
}
}
}

最佳答案

我认为问题在于您的 UserRoleList 类,它是一个具有单个字段的类,您确实希望将其映射到简单类型。您可能想尝试使用 @XmlValue 注释来注释 userRoleSID 成员。

关于java - 如何从嵌套 XML 子元素中删除命名空间值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com