1

Given the @Controller below, even if i send a Get Request to the webApp, the controller run the homePage method.

@RestController
@RequestMapping(method = RequestMethod.POST)
public class MyController {

        @GetMapping("/hello")
        public String homePage() {
            return "Hello, It is my first application";
        }
    }

How could that happen? Normally, i restrict that from the class level.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
xmen-5
  • 1,806
  • 1
  • 23
  • 44
  • 1
    They are agregated (not limited). So when doing a `value` it is added `method` is also added so now bot GET and POST are supported. – M. Deinum Jul 08 '19 at 13:24

1 Answers1

1

Your method with @GetMapping("/hello") picked up as most specific and enables GET requests with /hello path

This annotation can be used both at the class and at the method level. In most cases, at the method level applications will prefer to use one of the HTTP method specific variants @GetMapping

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I tried to use @GetMapping() on the class level, but the compiler complains. – xmen-5 Jul 08 '19 at 13:31
  • @zakzak GetMapping is allowed only on methods https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/GetMapping.html – Ori Marko Jul 08 '19 at 13:31