Tôi đang cố gắng hiểu hành vi chính xác của Bộ điều khiển Phục hồi Dữ liệu Mùa xuân.Bộ điều khiển Phục hồi Dữ liệu Mùa xuân: hành vi và cách sử dụng của @BasePathAwareController, @RepositoryRestController, @Controller và @RestController
Tôi đã thực hiện một cài đặt đơn giản để kiểm tra 4 loại điều khiển chú thích: @BasePathAwareController
, @RepositoryRestController
, @RestController
, @Controller
Bộ điều khiển có một bản đồ cho một thực thể "tác giả" trong kho.
Đây là bộ điều khiển:
@BasePathAwareController
//@RepositoryRestController
//@RestController
//@Controller
public class MyController {
@RequestMapping(value="authors/mycontroller/test")
public void handleRequest(){
System.out.println("handleRequest of class MyController");
}
@RequestMapping(value="/authors/mycontroller/testslash")
public void handleSlashRequest(){
System.out.println("handleSlashRequest of class MyController");
}
@RequestMapping(value="api/authors/mycontroller/test")
public void handleApiRequest(){
System.out.println("handleApiRequest of class MyController");
}
@RequestMapping(value="/api/authors/mycontroller/testslash")
public void handleSlashApiRequest(){
System.out.println("handleSlashApiRequest of class MyController");
}
}
Tôi đang thử nghiệm 4 phương pháp bởi vì tôi có một số nghi ngờ về bản đồ quyền được sử dụng.
Đối với mỗi thử nghiệm, tôi sử dụng một bộ điều khiển khác với ánh xạ sames, chỉ cần giải thích chú thích mà tôi cần cho thử nghiệm đó.
Tôi gọi hai URL này với HTTP GET:
http://localhost:8080/myApp/api/mycontroller/test
http://localhost:8080/myApp/api/mycontroller/testslash
Đây là kết quả mà tôi được sự thoả sử dụng @BasePathAwareController
:
http://localhost:8080/myApp/api/mycontroller/test
White page, No errors, No print on console
http://localhost:8080/myApp/api/mycontroller/testslash
White page, No errors, No print on console
Đây là kết quả sử dụng @RepositoryRestController
:
http://localhost:8080/myApp/api/mycontroller/test
White page, No errors, No print on console
http://localhost:8080/myApp/api/mycontroller/testslash
White page, and this message on the console (only for the first HTTP GET on this url):
jul 27, 2016 9:23:57 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myApp/api/authors/mycontroller/testslash] in DispatcherServlet with name 'rest'
Đây là kết quả nếu tôi sử dụng @RestController
:
http://localhost:8080/myApp/api/mycontroller/test
White page, in console: "handleRequest of class MyController"
http://localhost:8080/myApp/api/mycontroller/testslash
White page, in console: "handleSlashRequest of class MyController"
Cuối cùng, đây là kết quả sử dụng @Controller
:
http://localhost:8080/myApp/api/mycontroller/test
HTTP STATUS 404, in console: "handleRequest of class MyController"
http://localhost:8080/myApp/api/mycontroller/testslash
HTTP STATUS 404, in console: "handleSlashRequest of class MyController"
For both urls I have this warning:
jul 27, 2016 9:28:11 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myApp/api/authors/mycontroller/authors/mycontroller/test] in DispatcherServlet with name 'rest'
Tôi không hiểu những gì đang xảy ra.
Chú thích duy nhất dường như cung cấp kết quả mong đợi là @RestController
.
@Controller
dường như có vẻ hoạt động, nhưng tôi có trạng thái HTTP 404 và có thông báo có URL không nhất quán /myApp/api/authors/mycontroller/authors/mycontroller/test
, mặc dù có thông báo xử lý chính xác trên bảng điều khiển.
Hai chú thích khác (@BasePathAwareController
và @RepositoryRestController
) không làm gì cả.
Nói tóm lại:
@BasePathAwareController
: không làm gì cả@RepositoryRestController
: không làm gì cả@Controller
: Hành vi lạ@RestController
: hoạt động hoàn hảo
Nó sẽ được đánh giá cao bất kỳ loại clarifica tion về hành vi và cách sử dụng của mọi loại @Controller.
Cảm ơn.
Hãy giải thích lý do tại sao các tài liệu nói "Bộ điều khiển này (@RepositoryRestController) sẽ được phân phát từ cùng một đường dẫn cơ sở API được định nghĩa trong RepositoryRestConfiguration.setBasePath được tất cả các thiết bị đầu cuối RESTful khác sử dụng (ví dụ:/api)". Và đoạn mã KHÔNG có @RequestMapping ở cấp lớp. https://docs.spring.io/spring-data/rest/docs/3.0.1.RELEASE/reference/html/#customizing-sdr.overriding-sdr-response-handlers (chương 15.4) –
sai, họ sẽ được xử lý từ lớp đối tượng miền đa nguyên hoặc từ đường dẫn bạn đã chỉ định trong 'RepositoryRestResource (path =' – Sam
Nó có thể phụ thuộc vào phiên bản bạn đang sử dụng. Với phiên bản tôi đã sử dụng trong thời gian đó tôi đã giải quyết theo cách này. –