简体   繁体   English

如何在Spring中将具有相同前缀uri的映射重载到不同的方法?

[英]How to overload mapping with same prefix uri to different methods in Spring?

I'm trying to map two different URI with the same prefix to two different handlers 我正在尝试将具有相同前缀的两个不同URI映射到两个不同的处理程序

POST "resource" -> resourcesBl.create POST“资源”-> resourcesBl.create

POST "resource/{path_variable}" -> resourcesBl.createOther POST“资源/ {path_variable}”-> resourcesBl.createOther

note - the request body for each one is different 注意-每个请求的主体都不同

and Spring doesn't do it automatically 而且Spring不会自动执行

@RestController
@RequestMapping("resource") 
public class MyController {

    @Autowired
    private ResourcesBl resourcesBl;

    @PostMapping
    public Resource create(@RequestBody Resource resource) {
        return resourcesBl.create(resource);
    }

    @PostMapping("/{resourceName}"}
    public OtherResource create(@PathVariable String resourceName, @RequestBody OtherResource otherResource, ) {
        return resourcesBl.createOther(otherResource);
    }

}

I have tried this solution with Postman, it works perfectly fine and takes the path variable, both mappings can be called with different bodies. 我已经在Postman上尝试过此解决方案,它的工作原理非常完美,并且采用path变量,可以使用不同的主体调用这两个映射。

@RestController
@RequestMapping("/resource")
public class MyController {

    @Autowired
    private ResourcesBl resourcesBl;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Resource create(@RequestBody Resource resource) {
        return resourcesBl.create(resource);
    }

    @RequestMapping(value = "/{resourceName}", method = RequestMethod.POST)
    @ResponseBody
    public OtherResource create(@PathVariable String resourceName,
                                @RequestBody OtherResource otherResource) {
        return resourcesBl.createOther(resourceName, otherResource);
    }
}

All requests with empty path params will be mapped to the first method, so both: "/resource" and "/resource/". 所有路径参数为空的请求都将映射到第一个方法,因此都为“ / resource”和“ / resource /”。 Anything after the slash to the second. 斜线之后的所有内容。

Note: don't forget to use the path variable. 注意:不要忘记使用path变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在spring中基于请求体将相同的URL映射到不同的方法 - Mapping the same url to different methods based on request body in spring 是否可以在 Spring/REST 中定义具有相同映射但不同参数的多桩方法? - Is it possible to define multpile methods with the same mapping but different paramters in Spring/REST? 如何重载使用@RequestBody 的 Spring 引导 RestController 方法? - How to overload Spring Boot RestController methods that use @RequestBody? 如何使用不同的anotations创建两个映射方法 - How to create two mapping methods with different anotations Spring MVC URI映射配置 - Spring MVC URI mapping configuration spring http请求映射单个uri用不同的请求参数到不同的方法是好的做法还是不好的做法 - spring http request mapping single uri with different request parameters to different method is good practice or bad practice REST API-对不同的参数使用相同的URI和相同的方法来调用不同的方法 - REST API- use same URI & same method for different parameter to call different methods 如何为不同的方法使用不同的 spring 事务 - How to use different spring transactions for different methods Spring MVC-如何使用不同的映射注释调用相同的URL - Spring MVC - How do I call the same URL with different mapping annotations 两种不同的URI映射相同的视图,一种效果很好,但是不能为另一种加载css - two different URI mapping same view, one works well, however css can not be loaded for the other one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM