0

My interface:

package com.demo.dependency;

@RestController
@RequestMapping(value = "#{'${api.baseUrl}'}")
public interface BaseController<Response> {

    @PostMapping(value = "#{'${api.interface}'}")
    @ResponseBody
    public Response process(@RequestBody @Valid Request request) throws Exception;

}

Implementation:

package com.demo.application;

public class BarController implements BaseController<BarResponse> {

    @Override
    public BarResponse process(Request request) throws Exception {
        // do something
    }

}

I'm new to Spring Boot. I wonder whether these annotations can work properly in implementation class:

  • @RestController and @RequestMapping on interface class
  • @PostMapping and @ResponseBody on interface method
  • @RequestBody and @Valid on method parameters
  • #{'${api.baseUrl}'} and #{'${api.interface}'} to read configure from application.properties

My spring boot version is 2.2.6.

It seems that my SpringApplication from package com.demo.application failed to auto scan BarController. However, this answer says that "the annotation should apply to all subclasses", including @Service. Is anything wrong with my code?

Thanks in advance.

cosimoth
  • 167
  • 10
  • What version are you using? – Unmitigated Jul 28 '20 at 03:16
  • 1
    Annotations aren't inherited from interfaces to classes in java, so the `@RestController` needs to be on the implementation class. The `@RequestMapping` might work due to the annotation processing in Spring itself. – M. Deinum Jul 28 '20 at 04:51
  • 1
    `#{'${api.baseUrl}'}` and `#{'${api.interface}'}` will work. `@Valid` and `@RequestBody` will not work. Other are already answered by M. Deinum. – YoManTaMero Jul 28 '20 at 06:55

0 Answers0