java - 未找到 com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector --- 使用 Swagger 时

标签 java maven swagger glassfish-4.1

我在 SO 上的第一篇文章并期待好的返回:-)

我开发了一个小型 Java Restful 服务应用程序并与 Swagger 集成。我有 @Controller -> @Service -> @Repository 架构。我已经部署在 Glassfish (4.1.1) 上,当我使用 Chrome 的“高级休息客户端”时,我能够完美地发送和接收休息调用(GET/POST 等),但是当我使用 Swagger 时,它会抛出以下异常 '在 Controller 返回正确的响应之后'。

我一直在通过更改 maven 条目版本、更改 moxy jar、按照某些论坛中的建议删除 felix 等来解决这个问题,但似乎没有任何帮助。

这里有更多的细节......

异常:

  StandardWrapperValve[com.xxx.config.ApplicationConfig]: Servlet.service() for servlet com.xxx.config.ApplicationConfig threw exception
java.lang.ClassNotFoundException: com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector not found by com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider [130]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

代码:

@Controller
@Path("/document")
@Api(value = "/document", description = "Document Controller ")
public class DocumentController {

    @Inject
    DocumentService documentService;

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get Document.", notes = "Get Document Call")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 500, message = "Something wrong in Server") })
    public DtoDocument getDocument(@PathParam("id") Integer docId) {
        Document doc = documentService.getDocument(docId);
        DtoDocument dto = toDto(doc);
        return dto;
    }
}

@Service
@Transactional
public class DocumentService {

    @Inject
    DocumentRepository repository;

    public Document getDocument(Integer id) {
        return repository.getDocumentById(id);
    }

}

@Repository
public class DocumentRepository {

    public static final String COLLECTION_NAME = "document";

    @Inject
    private MongoTemplate mongoTemplate;

    public Document getDocumentById(Integer Id) {
        Document doc = getMongoTemplate().findOne(Query.query(Criteria.where("id").is(Id)), Document.class, COLLECTION_NAME);
        return doc;
    }

}

@ApplicationPath("/rest")
public class ApplicationConfig extends Application {

    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet();

        resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResource.class);
        resources.add(com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider.class);
        resources.add(com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON.class);
        resources.add(com.wordnik.swagger.jaxrs.listing.ResourceListingProvider.class);

        addRestResourceClasses(resources);

        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources) {
        // Custom resources in the project (all restful services)
        resources.add(com.xxx.web.rest.DocumentController.class);
    }

}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    bean-discovery-mode="all">
</beans>

pom.xml 中的 swagger 条目

<dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-jersey-jaxrs_2.10</artifactId>
            <version>1.3.13</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jersey.contribs</groupId>
                    <artifactId>jersey-spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-server</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-servlet</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-multipart</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.codehaus.jackson</groupId>
                    <artifactId>jackson-mapper-asl</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.module</groupId>
                    <artifactId>jackson-module-jaxb-annotations</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

如果有人能告诉我到底是什么问题,我将不胜感激……这是令人沮丧的一周,我一直坚持下去,没有取得任何进展。

最佳答案

堆栈跟踪提到:java.lang.ClassNotFoundException: com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector not found by com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider

并且您排除了 JAXB 注释模块:

<exclusion>
   <groupId>com.fasterxml.jackson.module</groupId>
   <artifactId>jackson-module-jaxb-annotations</artifactId>
</exclusion>

排除意味着没有解决依赖关系,因此您将得到 ClassNotFoundException

关于java - 未找到 com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector --- 使用 Swagger 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538801/

相关文章:

Java - 从另一个类调用方法[错误]

java - slf4j 记录器日志级别未正确继承

java - 使用 Maven 为每个环境定制 context.xml

java - IntelliJ : scala: javac: invalid source release: 1. 7

java - Swagger UI - 可为空值

java - 通过 Spring Social 获取完整 Linkedin 个人资料时出现 400 错误请求错误

java - 如何在不接收来自 Flex 客户端的消息的情况下从 BlazeDS 推送数据?

java - 设置操作系统环境变量后,Apache Maven install "' mvn' not Recognized as an internal or external command"?

java - Swagger 与 MapStruct 抛出 org.springframework.context.annotation.ConflictingBeanDefinitionException

authentication - 使用 Swagger 3.0 发送 cookie session ID