为什么RequestMapping注释不能在springboot应用程序中使用类?

时间:2017-08-16 13:21:22

标签: java spring-boot spring-restcontroller request-mapping

我正在尝试用样本学习springbott。当我试图调用在类上定义的restcontroller时,我收到404错误。

@RestController
@RequestMapping("/test")
public class ReservationResource {

    public ResponseEntity<ReservationResponse> getAvaliableRooms(
            @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) LocalDate checkin,
            @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) LocalDate checkout) {

        return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
    }
}

此应用程序没有/ error的显式映射,因此您将此视为回退。 8月16日星期三16:18:05 EEST 2017 出现意外错误(type = Not Found,status = 404)。 没有可用消息

但是,当我尝试下面的时候它有效!

@RestController
public class ReservationResource {

    @RequestMapping("/test")
    public ResponseEntity<ReservationResponse> getAvaliableRooms(
            @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) LocalDate checkin,
            @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) LocalDate checkout) {

        return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
    }
}

为什么会出现这种差异?顺便说一下,我按照一个例子,开发人员将其定义为第一部分!

4 个答案:

答案 0 :(得分:2)

您必须使用@RequestMapping注释您的方法。如果没有注释,Controller无法知道应该执行哪种方法。

@RequestMapping("/test")
Class Level

@RequestMapping
Mehtod Level

Reachable = http://localhost:8080/test
@RequestMapping("/test")
Class Level

@RequestMapping("/abc")
Mehtod Level

Reachable = http://localhost:8080/test/abc
@RequestMapping("/test")
Class Level

@RequestMapping("/abc")
Mehtod Level

@RequestMapping("/def")
Mehtod Level

Reachable = http://localhost:8080/test/abc
Reachable = http://localhost:8080/test/def

答案 1 :(得分:1)

因为如果你在类级使用@RequestMapping,它将不知道要执行哪个方法,因为你的类可以有多个方法

答案 2 :(得分:1)

使用注释时@RestController意味着您的类将充当@Controller,并且内部定义的所有方法默认都具有@ResponseBody

但您需要为Controller中的每个方法定义特定路径。

例如你可以这样做:

@RestController
@RequestMapping("/test")
public class ReservationResource {

    @RequestMapping("/") 
    public ResponseEntity<ReservationResponse> getAvaliableRooms(
            @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) LocalDate checkin,
            @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) LocalDate checkout) {

        return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
    }

    @RequestMapping("/second") 
    public ResponseEntity<ReservationResponse> getAvaliableRooms(
                @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) LocalDate checkin,
                @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) LocalDate checkout) {

            return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
        }
    }

使用此配置,您已使用GET方法在/ test /中定义了路径,而在GET方法中定义了其他“/ test / second”,但是聋人路径将以“/ test”开头

如果您决定在方法级别使用@RequestMapping,则可以修改同一类中的路径。

例如:

@RestController
public class ReservationResource {

        @RequestMapping("/test") 
        public ResponseEntity<ReservationResponse> getAvaliableRooms(
                @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) LocalDate checkin,
                @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) LocalDate checkout) {

            return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
        }

        @RequestMapping("/second") 
        public ResponseEntity<ReservationResponse> getAvaliableRooms(
                    @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) LocalDate checkin,
                    @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) LocalDate checkout) {

                return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
            }
        }

这两个网址都可以通过“/ test”和“/ second”访问

答案 3 :(得分:1)

/test是控制器的类级别映射,然后为了使getAvaliableRooms() public方法符合条件,可以为您的@RequestMapping("")添加getAvaliableRooms() {1}}方法如下所示:

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("")
    public ResponseEntity<ReservationResponse> getAvaliableRooms(
        @RequestParam(value = "checkin") @DateTimeFormat(iso = ISO.DATE) 
                   LocalDate checkin,
        @RequestParam(value = "checkout") @DateTimeFormat(iso = ISO.DATE) 
               LocalDate checkout) {

       return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
    }
}

换句话说,您明确告诉Spring容器控制器类中的哪个public方法实际上是用于提供请求。