SpringBoot 1.5.10.RELEASE REST服务持续返回404求助
Hey there, I know it's frustrating when all the common fixes don't work—let's dig into your setup step by step. First, let's recap what you shared to align on the problem:
Your REST Controller Setup
@RestController @RequestMapping("/api") public class HmlRestController { // ... other code ... @RequestMapping(value = "/myResource/", method = RequestMethod.POST) public ResponseEntity<?> myResource(@RequestBody HmlEvent hmlEvent) { // ... endpoint logic ... } }
You mentioned you have an application.properties file too, so let's go through targeted checks to find the root cause:
Double-check the request path & HTTP method
Your endpoint is mapped to/api/myResource/(note the trailing slash). Make sure your client is sending requests to this exact path—some tools or frameworks automatically strip trailing slashes, which would result in a 404. Test both/api/myResource/and/api/myResourceto rule this out. Also, confirm you're definitely using a POST request (not GET/PUT/DELETE by mistake).Validate request body deserialization
Spring needs to convert your incoming JSON to anHmlEventobject, and this is a common failure point:- Ensure
HmlEventhas a no-argument constructor—Jackson (Spring's default serializer) requires this to instantiate the class. - Verify field names match exactly between your JSON request body and
HmlEvent(case-sensitive!). If you're using different names, add@JsonProperty("json-key-name")annotations to the corresponding fields. - Add validation to catch errors: Modify your method signature to include
BindingResultlike this:public ResponseEntity<?> myResource(@Valid @RequestBody HmlEvent hmlEvent, BindingResult result) { if (result.hasErrors()) { // Return error details to debug what's wrong with the request body return ResponseEntity.badRequest().body(result.getAllErrors()); } // ... your existing logic ... }
- Ensure
Confirm Spring is scanning your controller
Make sure your main application class (annotated with@SpringBootApplication) is in a parent package of your controller. For example, if your controller is incom.yourapp.controller, your main class should be incom.yourapp(or use@ComponentScan(basePackages = "com.yourapp.controller")explicitly if the package structure is non-standard).Check for context path overrides
Look in yourapplication.propertiesfor a line likeserver.servlet.context-path=/custom-path. If this exists, your full request URL needs to include this prefix (e.g.,/custom-path/api/myResource/).Inspect deployment & logs
- Verify your WAR is deployed successfully on your server (e.g., Tomcat)—check the server's deployment logs for any errors during startup.
- Enable debug logging to see exactly what's happening with requests: Add these lines to
application.properties:
The logs will show if the request is reaching your controller, if there's a mapping mismatch, or if deserialization is failing.logging.level.org.springframework.web=DEBUG logging.level.com.yourpackage=DEBUG
Hopefully one of these checks uncovers the issue—let me know if you find any specific errors in logs or have more details to share!
内容的提问来源于stack exchange,提问作者user6882156

