Announcement Announcement Module
Collapse
No announcement yet.
Spring-DATA-JPA and REST: No rest ressources available Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring-DATA-JPA and REST: No rest ressources available

    Hi!

    I am trying to build a very simple DATA REST application.
    I copied most of it from the sample application.

    When I run it in STS it loads without any errors.

    BUT:

    I cannot access any ressources.
    With "curl" I get this result:

    Code:
    D:\curl>curl -v http://localhost:8080/test1/
    * About to connect() to localhost port 8080 (#0)
    *   Trying 127.0.0.1...
    * connected
    * Connected to localhost (127.0.0.1) port 8080 (#0)
    > GET /test1/ HTTP/1.1
    > User-Agent: curl/7.27.0
    > Host: localhost:8080
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Apache-Coyote/1.1
    < X-TraceId: 5069d870-30
    < X-TraceUrl: /insight/services/traces/5069d870-30?type=json
    < Content-Type: application/json
    < Content-Length: 41
    < Date: Mon, 01 Oct 2012 18:41:16 GMT
    <
    {
      "links" : [ ],
      "content" : [ ]
    }* Connection #0 to host localhost left intact
    * Closing connection #0
    I attach the project as zip file. Maybe you can identfy my problem and help me?


    Attachment
    Attached Files

  • #2
    There are basically two ways to set up the application so that your Repositories are properly discovered in the right ApplicationContext.

    1) As separate config classes.

    In the initializer in the spring-data-rest/spring-data-rest-webmvc tests [1] the JPA Repository configuration is split out into an ApplicationConfig class that has the @EnableJpaRepositories annotation on it but does not have the @Import(RepositoryRestMvcConfiguration.class) annotation. It is simply a JPA and Repository configuration. There is nothing web or REST-related in this config class. There is a second JavaConfig class named ApplicationRestConfig that extends RepositoryRestMvcConfiguration and adds REST-related components that is used as the dispatcher servlet's config. You can also just @Import(RepositoryRestMvcConfiguration.class).

    2) As a single config.

    The sample application has a single config that has both @EnableJpaRepositories and @Import(RepositoryRestMvcConfiguration.class) annotations on it. This class is used as the dispatcher servlet's ApplicationContext. In this configuration, it's important to note that there is no servlet context-level application config. [2]

    [1] - https://github.com/SpringSource/spri...itializer.java
    [2] - https://github.com/SpringSource/spri...EB-INF/web.xml
    Last edited by Jon Brisbin; Oct 2nd, 2012, 09:56 AM.

    Comment


    • #3
      Hi Jon,

      thanks for your help! It is really appreaciated!

      I revised my config file. I even tried to use a web.xml instead of my WebApplicationInitializer. But I allways get the same output from curl:
      a JSON response but without any resources listed...

      I am using STS 3.0.0.RELEASE and I am trying the single config version.


      I am posting my config files. Maybe you (or someone else) can find the problem!?


      Thanks!
      Dominik

      Code:
      package de.cab.test1;
      
      import javax.servlet.ServletContext;
      import javax.servlet.ServletException;
      import javax.servlet.ServletRegistration;
      
      import org.springframework.web.WebApplicationInitializer;
      import org.springframework.web.context.ContextLoaderListener;
      import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
      import org.springframework.web.servlet.DispatcherServlet;
      
      import de.cab.test1.config.AppConfig;
      
      public class AppInitializer implements WebApplicationInitializer {
      
      	public void onStartup(ServletContext servletContext) throws ServletException {
      /*
      Tried it with and without rootContext...
         	    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
      	    rootContext.register(AppConfig.class);
      
      	    // Manage the lifecycle of the root application context
      	    servletContext.addListener(new ContextLoaderListener(rootContext));
      	    
      */	    
      		 DispatcherServlet servlet = new DispatcherServlet();
      		 servlet.setContextClass(AnnotationConfigWebApplicationContext.class);
      		 servlet.setContextConfigLocation(AppConfig.class.getName());
      		 ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", servlet);
      		 dispatcher.setLoadOnStartup(1);
      		 dispatcher.addMapping("/*");
      
      	}
      
      }
      and

      Code:
      package de.cab.test1.config;
      
      import javax.persistence.EntityManagerFactory;
      import javax.sql.DataSource;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.context.annotation.Import;
      import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
      import org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration;
      import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
      import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
      import org.springframework.orm.jpa.JpaDialect;
      import org.springframework.orm.jpa.JpaTransactionManager;
      import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
      import org.springframework.orm.jpa.vendor.Database;
      import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
      import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
      import org.springframework.transaction.PlatformTransactionManager;
      import org.springframework.transaction.annotation.EnableTransactionManagement;
      
      import de.cab.test1.domain.User;
      
      @Configuration
      @Import(RepositoryRestMvcConfiguration.class)
      @ComponentScan(basePackageClasses = User.class)
      @EnableJpaRepositories
      @EnableTransactionManagement
      public class AppConfig {
      
        @Bean public DataSource dataSource() {
          EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
          return builder.setType(EmbeddedDatabaseType.HSQL).build();
        }
      
        @Bean public EntityManagerFactory entityManagerFactory() {
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setDatabase(Database.HSQL);
          vendorAdapter.setGenerateDdl(true);
      
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setPackagesToScan("de.cab.test1.domain");
          factory.setDataSource(dataSource());
      
          factory.afterPropertiesSet();
      
          return factory.getObject();
        }
      
        @Bean public JpaDialect jpaDialect() {
          return new HibernateJpaDialect();
        }
      
        @Bean public PlatformTransactionManager transactionManager() {
          JpaTransactionManager txManager = new JpaTransactionManager();
          txManager.setEntityManagerFactory(entityManagerFactory());
          return txManager;
        }
      
      }

      Comment


      • #4
        What application server (and version) are you deploying to in STS?

        If you're using Maven, have you tried including the Jetty 8 plugin and trying "mvn jetty:run" from the project directory to see if that gives different results?

        Comment


        • #5
          Hi Jon,

          i am using plain STS with the VMware vFabric tc Server Developer Edition v2.7
          I installed Jetty and got the same results as with the tc Server...

          Judging from your question my config should be ok, no?

          Thanks
          Dominik

          Comment


          • #6
            This is, in fact, the exact setup I'm using to develop my SpringOne demo application. I'm also using Maven in this example project.

            Let me get a skeleton of this set up and put it on my GitHub. Then I'd be interested to see if you can load it in your setup like I can.

            Comment


            • #7
              Hi Jon,

              it's me again... I got it running now! But: It only worked after I packed all files in the same class.

              You may allready have noticed that I am no pro in Java/Spring
              Could you have a look at my config and see if all the path/class references are ok? I think they should be ok...

              Thanks again!

              Comment

              Working...
              X