Paddy
Paddy

Reputation: 3630

Spring boot with resteasy - "could not find the type for bean named requestMappingHandlerMapping" error


Does anybody know hwo to configure Spring boot to use resteasy, specifically when I do not want embedded container? We have a standalone JBoss where we need to be able to deploy the application, but I use a Jetty container for tests for testing the URL mappings. My build.gradle is like this (includes aop, security etc. just for future use, hope that does not impact):

    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')
}

and my Application.java is as follows:

@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);
    }
}

and my ApplicationTests.java is as follows:

@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 documentation says it that it imports all handler mappings but still i get this error when the test runs

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

Does anyone know why this could be and what can be done?

Thanks,
Paddy

Upvotes: 2

Views: 3248

Answers (2)

Fabio
Fabio

Reputation: 126

You can use RESTEasy Spring Boot starter. See how to do it below. Now talking about using RESTEasy without an embedded container, do you mean by not having a Servlet container at all, or by building your Spring Boot app as a WAR, and then deploying it to a regular standalone container? RESTEasy depends on a Servlet container to function, since its REST endpoints are wired to HTTP via a front controller Java servlet.

Instructions to use Spring Boot RESTEasy starter below

Adding POM dependency

Add the Maven dependency below to your Spring Boot application pom file.

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

Registering JAX-RS application classes

Just define your JAX-RS application class (a subclass of Application) as a Spring bean, and it will be automatically registered. See the example below. See section JAX-RS application registration methods in How to use RESTEasy Spring Boot Starter for further information.

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 {
}

Registering JAX-RS resources and providers

Just define them as Spring beans, and they will be automatically registered. Notice that JAX-RS resources can be singleton or request scoped, while JAX-RS providers must be singletons.

Further information at the project GitHub page.

Upvotes: 0

Erik Schmiegelow
Erik Schmiegelow

Reputation: 2759

I'm not aware of the specifics of Spring Boot, but I've configured Spring with resteasy several times and I suspect that it will be the same. You will have to wire resteasy and spring in web.xml to ascertain that resteasy is loaded from spring's context.

web.xml looks like this:

<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>-->

Upvotes: 1

Related Questions