有谁知道如何配置 Spring Boot 以使用 resteasy,特别是当我不想要嵌入式容器时?我们有一个独立的 JBoss,我们需要能够在其中部署应用程序,但我使用 Jetty 容器来测试 URL 映射。我的build.gradle是这样的(包括aop,security等,只是为了以后使用,希望不影响):
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')
}
我的Application.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);
}
}
我的ApplicationTests.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
有谁知道为什么会这样以及可以做什么?
谢谢,
水稻
最佳答案
我不知道 Spring Boot 的具体细节,但我已经用 resteasy 配置了 Spring 几次,我怀疑它会是一样的。您必须在 web.xml 中连接 resteasy 和 spring 以确定 resteasy 是从 spring 的上下文中加载的。
web.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>-->
关于java - 带有 resteasy 的 Spring 启动 - "could not find the type for bean named requestMappingHandlerMapping"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27266345/