2

I have created simple Spring BOOT application with Spring Data Rest. It works fine with repositories. However, I need to add some extra functionality and for that purpose would like to use controller class annotated with @RepositoryRestController. The issue is when I use @RestController annotation GET method is available for calling but when I switch annotation to @RepositoryRestController browser says the method is not available.

POM file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Controller class:

@RepositoryRestController
public class HelloContoller {

    @RequestMapping("/test")
    @ResponseBody
    public String sayHello() {
        return "Hi, there!";
    }

    @RequestMapping(value = "test", method = RequestMethod.POST)
    public String postHello() {
        return "POST!";
    }

    @RequestMapping(value = "/test", method = RequestMethod.PUT)
    public String putHello() {
        return "PUT!";
    }

}

Configuration class:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

I am new to Spring Data Rest and would appreciate any help!

Thanks!

Marc Tarin
  • 3,109
  • 17
  • 49
Yury K
  • 51
  • 6

2 Answers2

0

In case of overriding just some methods of the ones generated by spring-data-rest's RepositoryEntityController I also had problems that you ran into.

It seems that in such cases you cannot use the class level @RequestMapping annotation on a RepositoryRestController.

So remove @RequestMapping("/test") from the class and repeat the /test path on each method.

Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
0

Finally, I have managed to find answer with help from Vladimir Tsukur. In order to use @RepositoryRestController properly you must create repository interface first and then use its URL as base path for @RequestMapping on each method of the controller. Now everything works fine. Just be careful about the URLs :)

Yury K
  • 51
  • 6