java+nojaxbclassmap_java-Jersey REST / JAXB错误,映射接口

我必须在REST Web服务中使用接口.这是接口Specs.java:

@XmlJavaTypeAdapter(MyAdapter.class)

public interface Specs {

public BaseProperties getBaseProps();

public void setBaseProps(BaseProperties baseProps);

}

MyAdapter.java:

public class MyAdapter extends XmlAdapter

{

public Object unmarshal(Object v)

{

return v;

}

public Object marshal(Object v)

{

return v;

}

}

RegSpecs.java

@XmlType

public class RegSpecs implements Specs{

private BaseProperties baseProps;

public BaseProperties getBaseProps()

{

return baseProps;

}

public void setBaseProps(BaseProperties baseProps)

{

this.baseProps = baseProps;

}

}

MapSpecs.java

@XmlType

public class MagSpecs implements Specs {

private BaseProperties baseProps;

private Features features;

public BaseProperties getBaseProps()

{

return baseProps;

}

public void setBaseProps(BaseProperties baseProps)

{

this.baseProps = baseProps;

}

public Features getFeatures() {

return features;

}

public void setFeatures(Features features) {

this.features = features;

}

}

访问此服务将引发以下错误:

javax.xml.bind.MarshalException

– with linked exception:

[javax.xml.bind.JAXBException: class entities.MagSpecs nor any of its super class is known to this context.]

如何修改我的上下文?我正在使用与Jersey 1.5捆绑在一起的JAXB

谢谢 !

编辑:为了尝试更新我的上下文,我将此代码添加到我的客户端(资源)类中:

public class BookService implements ContextResolver

{

private JAXBContext jaxbContext;

public BookService() {

try {

// Bootstrap your JAXBContext will all necessary classes

jaxbContext = JAXBContext.newInstance(Specs.class,MagSpecs.class, RegSpecs.class);

} catch(Exception e) {

throw new RuntimeException(e);

}

}

public JAXBContext getContext(Class> clazz) {

if(BookService.class == clazz) {

return jaxbContext;

}

return null;

}

在这种情况下,我会报错:

entities.Specs is an interface, and JAXB can’t handle interfaces.

this problem is related to the following location:

at entities.Specs

entities.Specs does not have a no-arg default constructor.

this problem is related to the following location:

at entities.Specs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可能是由于缺少相关的依赖库导致的。在Java中,Jakarta Servlet是Java Servlet API的新名称,它提供了与Web应用程序相关的类和接口。而Spring Boot是基于Servlet容器的Web框架,因此它需要使用javax.servlet.Servlet和相关的类和接口。 你需要确保你的项目中已经包含了相关的依赖库。如果你使用的是Spring Boot 2.x版本,可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 如果你使用的是Spring Boot 1.x版本,可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 如果你已经添加了这些依赖库,但是还是出现这个问题,可能是因为你的IDE没有正确配置classpath。你可以尝试重新导入项目或者重新启动IDE来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值