简体   繁体   English

Spring Boot - 模糊映射

[英]Spring Boot - Ambiguous Mapping

Pulling my hair out over this one, makes absolutely no sense把我的头发拉到这个上面,完全没有意义

@RestController("/firmwareAShkcwdsdskl")
public class FirmwareController {
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> createNewFirmwareVersion(DetailedFirmwareVersionRequest detailedFirmwareVersionRequest) { 
    // Code Block
    }
}

@RestController("/jobs/firmwareUpgrade")
public class FirmwareUpgradeController {
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> createNewJob(DetailedFirmwareUpgradeRequest detailedFirmwareUpgradeRequest) {
    // Code Block
    }
}

Attempting to start my Spring Boot application with these two controllers is throwing the following error尝试使用这两个控制器启动我的 Spring Boot 应用程序会引发以下错误

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/jobs/firmwareUpgrade' method 
eshepherd.admin.api.controller.FirmwareUpgradeController#createNewJob(DetailedFirmwareUpgradeRequest)
to {POST , produces [application/json]}: There is already '/firmwareAShkcwdsdskl' bean method
eshepherd.admin.api.controller.FirmwareController#createNewFirmwareVersion(DetailedFirmwareVersionRequest) mapped.

The class name, method name, request mapping and parameters are all unique, I just don't understand it.类名、方法名、请求映射和参数都是唯一的,我就是不明白。

As you can tell I've descended into pure frustration trying all kinds of string changes to just get it to work initially, but if anyone could help me identify the problem I'd be extremely grateful.正如您所看到的,我在尝试各种字符串更改以使其最初正常工作时陷入了纯粹的挫败感,但如果有人能帮助我确定问题,我将不胜感激。

Edit: Using Spring-Boot 2.2.4编辑:使用 Spring-Boot 2.2.4

As you mentioned in your own answer: Yes, you misunderstood.正如您在自己的回答中提到的:是的,您误解了。 This is an easy one to confuse.这是一个容易混淆的问题。

@RestController 's value is the component name, not the request mapping path. @RestController的值是组件名称,而不是请求映射路径。 Take a look at the source for org.springframework.web.bind.annotation.RestController#value .查看org.springframework.web.bind.annotation.RestController#value的源代码。

It is very similar to org.springframework.stereotype.Component#value and the others in org.springframework.stereotype (in spring-context ).它与org.springframework.stereotype.Component#valueorg.springframework.stereotype (在spring-context )中的其他人非常相似。 You have have the correct annotations now:您现在拥有正确的注释:

@RestController
@RequestMapping("/firmware")
public class FirmwareRestController {
    // ...
}

Okay.. I managed to get it compiling.. I think I was misunderstanding how you can use the @RestController annotation好的..我设法让它编译..我想我误解了如何使用@RestController注释

If you provide如果你提供

@RestController
@RequestMapping("/firmware")

Instead of代替

@RestController("/firmware")

It will build.它会建立。 I guess I had assumed the default argument on the controller would be more useful, and looked to be working as I expected from the build messages.我想我已经假设控制器上的默认参数会更有用,并且看起来像我从构建消息中预期的那样工作。

Hope this helps someone!希望这对某人有帮助!

The framework is complaining that there exists a post path / from the 2 controllers where the mapping is not specified for any one of them hence it cannot distinguish the exact path.该框架抱怨存在来自 2 个控制器的 post 路径/ ,其中没有为其中任何一个指定映射,因此它无法区分确切的路径。

Can you please try the following code:你可以试试下面的代码:

@RestController
@RequestMapping("/firmwareAShkcwdsdskl")
public class FirmwareController {
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> createNewFirmwareVersion(DetailedFirmwareVersionRequest detailedFirmwareVersionRequest) { 
    // Code Block
    }
}


@RestController
@RequestMapping("/jobs/firmwareUpgrade")
public class FirmwareUpgradeController {
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result> createNewJob(DetailedFirmwareUpgradeRequest detailedFirmwareUpgradeRequest) {
    // Code Block
    }
}

@RequestMapping at controller level should define the root path for all the APIs in that path say for example I have to create a controller related to feeds API I would do:控制器级别的@RequestMapping应该为该路径中的所有 API 定义根路径,例如我必须创建一个与提要 API 相关的控制器,我会这样做:

@RestController
@RequestMapping("/api/feeds")
class FeedsController {
    //Constructor inject fields here
    @Autowired
    FeedsController() {

    }

    @GetMapping("/")
    ResponseEntity<Map<String, String>> get() {
        return ResponseEntity.ok(Collections.emptyMap());
    }

    @PostMapping("/")
    ResponseEntity<Map<String, String>> post() {
        return ResponseEntity.ok(Collections.emptyMap());
    }

    /**
     * Standard way to implement delete is to soft delete
     * @return
     */
    @DeleteMapping("/")
    ResponseEntity<Map<String, String>> delete() {
        return ResponseEntity.ok(Collections.emptyMap());
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM