Pregunta

I'm writing a rest service with spring-data-rest. And I face an exception that I don't know how to fix.

I have the following Application configuration

@Configuration
@ComponentScan(basePackageClasses = Application.class)
@EnableJpaRepositories
@EnableTransactionManagement
public class Application {

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().setType(H2).build();
    }

    @Bean
    public CustomerLoader loadCustomers() { 
        return new CustomerLoader();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("hello");
        return lef;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(true);
        hibernateJpaVendorAdapter.setDatabase(Database.H2);
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }

And This is my WebApplication initializer

@Override
    public void onStartup(ServletContext ctx)
            throws ServletException {

        AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
        rootCtx.register(Application.class);

        ctx.addListener(new ContextLoaderListener(rootCtx));

        RepositoryRestExporterServlet exporter = new RepositoryRestExporterServlet();

        ServletRegistration.Dynamic reg = ctx.addServlet("exporter", exporter);
        reg.setLoadOnStartup(1);
        reg.addMapping("/*");
    }

When I run my application on server I get the following exception on Servlet.init()

SEVERE: Servlet /spring-data-rest threw load() exception java.lang.NoSuchMethodError: org.springframework.data.rest.webmvc.ResourceProcessorInvokingHandlerAdapter.getReturnValueHandlers()Lorg/springframework/web/method/support/HandlerMethodReturnValueHandlerComposite; (if you want the full stacktrace please tell)

I thought it would be some class loading issue due to some jar duplication. But I'm building my project with maven and I'm using only one repository (http://repo.spring.io/libs-milestone) with parent pom spring-boot-starter-parent version 0.5.0.M5

¿Fue útil?

Solución 2

What I did was to remove the maven boot-starter-parent from my pom.xml and added all dependencies manually. I got a war with the exact same size, but it was working. Magic...

Otros consejos

It could be nothing but call my attention the name that you use for the package of the entities. I'm talking about the String "Hello" which the method "lef.setPackagesToScan" is receiving. Is this the real package where your entities are? If it is not, it could be the cause of your mistake.

For me updating to the latest snapshot version solved the issue.

<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top