有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有resteasy的java Spring引导“找不到名为requestMappingHandlerMapping的bean的类型”错误


有人知道hwo配置SpringBoot使用resteasy吗,特别是当我不需要嵌入式容器时?我们有一个独立的JBoss,需要在其中部署应用程序,但我使用Jetty容器来测试URL映射。我的身材。gradle是这样的(包括aop、安全等,仅供将来使用,希望不会影响):

    buildscript {
    ext {
        springBootVersion = '1.1.9.RELEASE'
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = <our package name>
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    compile.exclude(group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat')
    compile.exclude(group: 'org.springframework.boot', module: 'spring-boot-starter-logging')
}
dependencies {
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-aop")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.jboss.resteasy:resteasy-jaxrs:3.0.10.Final")
    compile('org.jboss.resteasy:resteasy-spring:3.0.10.Final')
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile('org.springframework.boot:spring-boot-starter-jetty')
}

还有我的申请。java如下所示:

@Configuration
@ComponentScan(basePackages = {"..."})     // our packages to scan
@EnableAutoConfiguration(exclude = { EmbeddedServletContainerAutoConfiguration.class })
@EnableWebMvc
@ImportResource("classpath:springmvc-resteasy.xml")
public class Application {
    public static void main(String[] args) {
        run(Application.class, args);
    }
}

还有我的应用测试。java如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@EnableAutoConfiguration
public class ApplicationTests {

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        return new JettyEmbeddedServletContainerFactory("/", 9000);
    }

    @Test
    public void contextLoads() {
    }
}

@EnableMvc文档说它导入了所有的处理程序映射,但是当测试运行时,我仍然得到这个错误

java.lang.IllegalStateException: could not find the type for bean named requestMappingHandlerMapping

有人知道为什么会这样,能做些什么吗

谢谢,
稻谷


共 (2) 个答案

  1. # 1 楼答案

    您可以使用RESTEasy Spring启动程序。请参见下面的操作方法。 现在说到在没有嵌入式容器的情况下使用RESTEasy,你的意思是根本没有Servlet容器,还是将Spring Boot应用程序构建为WAR,然后将其部署到常规的独立容器?RESTEasy依赖于一个Servlet容器来运行,因为它的REST端点通过前端控制器Java Servlet连接到HTTP

    下面是使用弹簧靴RESTEasy starter的说明

    添加POM依赖项

    将下面的Maven依赖项添加到Spring Boot应用程序pom文件中

    <dependency>
       <groupId>com.paypal.springboot</groupId>
       <artifactId>resteasy-spring-boot-starter</artifactId>
       <version>2.1.1-RELEASE</version>
       <scope>runtime</scope>
    </dependency>
    

    注册JAX-RS应用程序类

    只需将JAX-RS应用程序类(应用程序的子类)定义为Springbean,它就会自动注册。请参见下面的示例。有关更多信息,请参见How to use RESTEasy Spring Boot Starter中的JAX-RS应用程序注册方法

    package com.test;
    
    import org.springframework.stereotype.Component;
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @Component
    @ApplicationPath("/sample-app/")
    public class JaxrsApplication extends Application {
    }
    

    注册JAX-RS资源和提供商

    只需将它们定义为SpringBean,它们就会自动注册。请注意,JAX-RS资源可以是单例的,也可以是请求范围的,而JAX-RS提供者必须是单例的

    Further information at the project GitHub page

  2. # 2 楼答案

    我不知道Spring Boot的细节,但我已经用resteasy配置了Spring好几次,我怀疑它会是一样的。您将不得不在web中连接resteasy和spring。xml来确定resteasy是从spring的上下文加载的

    网络。xml如下所示:

    <web-app version="3.0" 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_3_0.xsd">
    
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.media.type.mappings</param-name>
        <param-value>html : text/html, json : application/json, xml : application/xml</param-value>
    </context-param> 
    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/</param-value>
    </context-param> 
    <! While using Spring integration set resteasy.scan to false or don't configure 
        resteasy.scan parameter at all 
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param> >