Scenario Description : When developing a project, due to internal conventions, the version and project must be indicated on the interface path, assuming that the version takes V1, Project: RMS, then all interface paths in the project are designed: http|https://ip:port| domain name/v1/ rms/*;
Because there are/v1/rms common parts of the path, I put it in a class that uses the annotation @restcontroller callout to indicate that it is a controller class, annotated with the annotation @requestmapping ("/v1/rms"). Then all the paths to the Controller class that inherit the class are prefixed with "/v1/rms" as the path.
1 Import org.springframework.web.bind.annotation.RequestMapping; 2 Import Org.springframework.web.bind.annotation.RestController; 3 4 5 @RestController 6 @RequestMapping ("/v1/rms")7publicclass Rmsbasecontroller {89 }
View Code
At the same time design another class inheriting it, the class inherits from the Rmsbasecontroller class, named TestController, assuming that all the interfaces defined in the class are prefixed with "/test", Naturally, Would have thought of annotating the class with @restcontroller @RequestMapping ("/test") just like the previous base class, but after doing so, the discovery program can start, but with "http://ip:port/v1/rms/test/*" access This class of interface will be reported 404 error, and can only use "http://ip:port/test/*" to access.
Error code :
1 Importorg.springframework.web.bind.annotation.RequestMapping;2 ImportOrg.springframework.web.bind.annotation.RequestMethod;3 ImportOrg.springframework.web.bind.annotation.RestController;4 ImportJavax.servlet.http.HttpServletResponse;5 6 @RestController7@RequestMapping ("/test")8 Public classTestControllerextendsRmsbasecontroller {9 Ten /** One * Add Test Interface ping A * */ -@RequestMapping (value = "Ping", method =requestmethod.get) - Public Final voidcheckapplication (httpservletresponse response) { theResponse.setstatus (200); - } -}View Code
Sample Request: Http://localhost:8090/vi/resource/test/ping
Error content:
<HTML><Body><H1>Whitelabel Error Page</H1><P>This application have no explicit mapping for/error, so is seeing this as a fallback.</P><DivID= ' created '>Wed Mar 19:38:20 CST 2018</Div><Div>There is an unexpected error (Type=not Found, status=404).</Div><Div>No message Available</Div></Body></HTML>
Modification Method :
Remove the TestController class annotation @requestmapping ("/test") and prefix "@requestmapping" with the value of/test for each of the TestController interfaces. Corrected code:
Import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.restcontroller;import javax.servlet.http.httpservletresponse;@ Restcontrollerpublic class TestController extends Rmsbasecontroller { /** * Add Test Interface Ping * */ @ Requestmapping (value = "/test/ping", method = requestmethod.get) public final void Checkapplication ( HttpServletResponse response) { response.setstatus ($); }}
View Code
Test Example: http://localhost:8090/v1/rms/test/ping
Result: Return status code: 200
Visible @RequestMapping ("/test") cannot annotate two classes that have an inheritance relationship at the same time.
Record the pits that have been trampled so that they can be bypassed later.
Spring @RequestMapping path cannot be inherited