Home > Software engineering >  Why when I use @PostMapping is the result correct, and when I replace it with @RequestMapping the re
Why when I use @PostMapping is the result correct, and when I replace it with @RequestMapping the re

Time:05-28

The code is here:

@ResponseBody
//@RequestMapping(name = "getScreenRecordListByHipJointEntity",method = RequestMethod.POST)
@PostMapping("getScreenRecordListByHipJointEntity")
public PageResult getScreenRecordListByHipJointEntity(@RequestBody HipJointVo hipJointVo) {
    return hipJointScreenService.getScreenRecordListByHipJointEntity(hipJointVo);
}

When I request this API which uses PostMapping annotation, the result code is ok and nothing is wrong, but when I replace the annotation @PostMapping("getScreenRecordListByHipJointEntity") with @RequestMapping(name = "getScreenRecordListByHipJointEntity",method = RequestMethod.POST), the HTTP request code is 404.

The requestBody has no changes. All the data is exactly the same.

And this is how I do the post request in postman

and this is how I do the post request in postman

CodePudding user response:

The problem is that you're using the name parameter of @RequestMapping, which isn't what you're intending. Instead, you should use value (which is the default parameter the value is applied to if you don't specify, as in your @PostMapping version) or path (which is a Spring alias to make the code more readable).

CodePudding user response:

@RequestMapping and @PostMapping's default parameter is value and value is aliasFor path. Both parameter are for path mapping.

@PostMapping("getScreenRecordListByHipJointEntity") == @RequestMapping(value = "getScreenRecordListByHipJointEntity", method = RequestMethod.POST)

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

  • Related