gpt4 book ai didi

jaxb - JAXB 编码对象 : javax. xml.bind.JAXBException : class . .. 或其任何父类(super class)的问题在此上下文中已知

转载 作者:行者123 更新时间:2023-12-04 23:07:15 24 4
gpt4 key购买 nike

在我的 JAX-RS 项目( Jersey )中,我在将 JAXB 注释的对象之一编码为 JSON 时遇到问题。这是我在日志中看到的错误消息:

SEVERE: Internal server error javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: class com.dnb.applications.webservice.mobile.view.CompaniesAndLocations nor any of its super class is known to this context.



这是否指向任何特定问题?我的资源有一个这样的方法:
@Path("/name/{companyname}/location/{location}")
@Produces("application/json; charset=UTF-8;")
@Consumes("application/json")
@POST
public Viewable findCompanyByCompanyNameAndLocationAsJSON(@PathParam("companyname") String companyName,
@PathParam("location") String location, CriteriaView criteria) {
criteria = criteria != null ? criteria : new CriteriaView();
criteria.getKeywords().setCompanyName(companyName);
return getCompanyListsHandler().listsByCompanyNameAndLocation(criteria, location);
}
Viewable是一个空界面。上述方法返回一个 CompaniesAndLocations 类型的对象,定义如下:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "companiesAndLocations", propOrder = { "count", "normalizedLocations", "companyList", "companyMap", "modifiers",
"modifiersMap", "companyCount", "navigators" })
public class CompaniesAndLocations extends BaseCompanies implements Viewable {

@XmlElement(name = "normalizedLocations", required = false)
protected List<NormalizedLocation> normalizedLocations;

public List<NormalizedLocation> getNormalizedLocations() {
if (normalizedLocations == null) {
normalizedLocations = new ArrayList<NormalizedLocation>();
}
return normalizedLocations;
}

}

BaseCompanies 定义了许多其他字段:
@XmlTransient
public abstract class BaseCompanies {

@XmlElement(name = "modifiers", required = false)
private List<Modifiers> modifiers;
....

我应该补充一点,我与应用程序中其他工作代码使用的方法略有不同。其他资源方法从用 @XmlRegistry 注释的 ObjectFactory 获取它们的对象。 .不过,我认为这没有必要。我已经看到其他代码直接实例化 JAXB 注释的 POJO,而不使用 ObjectFactory 工厂。

有任何想法吗?

最佳答案

JAX-RS 尽其所能引导 JAXBContext,但它并不总是能够访问它需要了解的所有类。为此,您可以实现 ContextResolver。这使您可以完全控制 JAXBContext 的创建方式。下面是它的外观示例:

package org.example.order;

import javax.ws.rs.Produces;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

@Provider
@Produces({"application/xml", "application/json"})
public class PurchaseOrderContextResolver implements ContextResolver<JAXBContext> {

private JAXBContext jaxbContext;

public PurchaseOrderContextResolver() {
try {
// Bootstrap your JAXBContext will all necessary classes
jaxbContext = JAXBContext.newInstance(PurchaseOrder.class);
} catch(Exception e) {
throw new RuntimeException(e);
}
}

public JAXBContext getContext(Class<?> clazz) {
if(PurchaseOrder.class == clazz) {
return jaxbContext;
}
return null;
}

}

关于jaxb - JAXB 编码对象 : javax. xml.bind.JAXBException : class . .. 或其任何父类(super class)的问题在此上下文中已知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4329659/

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