【问题标题】:Spring Boot wont create path defined in @RequestMapping(value="/path")Spring Boot 不会创建在 @RequestMapping(value="/path") 中定义的路径
【发布时间】:2021-09-26 12:00:27
【问题描述】:

我的 Spring Boot 应用有 3 个实体,带有 3 个控制器。 控制器如下所示:

@RestController

@CrossOrigin()

@RequestMapping(value="/userinfo")

public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping()
    public ResponseEntity<UUID> insertUser(@Valid @RequestBody UserDTO UserDTO) {
        UUID personID = userService.insert(UserDTO);
        return new ResponseEntity<>(personID, HttpStatus.CREATED);
    }

    @PutMapping()
    public ResponseEntity<UUID> insertIntoUser(@Valid @RequestBody UserDTO UserDTO) {
        UUID personID = userService.insert(UserDTO);
        return new ResponseEntity<>(personID, HttpStatus.CREATED);
    }

    @GetMapping()
    public ResponseEntity<List<UserDTO>> getUser() {
        List<UserDTO> dtos = userService.findUser();
        for (UserDTO dto : dtos) {
            Link personLink = linkTo(methodOn(UserController.class)
                    .getUserByID(dto.getId())).withRel("personDetails");
            dto.add(personLink);
        }
        return new ResponseEntity<>(dtos, HttpStatus.OK);
    }

    @GetMapping(value = "userid/{id}")
    public ResponseEntity<UserDTO> getUserByID(@PathVariable("id") UUID TenantID) {
        UserDTO dto = userService.findUserById(TenantID);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @GetMapping(value = "userrole/{role}")
    public ResponseEntity<UserDTO> getUserByRole(@PathVariable("role") int role) {
        UserDTO dto = userService.findUserByROle(role);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @GetMapping(value = "/userinfoname/{name}")
    public ResponseEntity<UserDTO> getUserByName(@PathVariable("name") String role) {
        UserDTO dto = userService.findUserByName(role);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @DeleteMapping(value = "deleteusername/{name}")
    public ResponseEntity<String> deleteUserByName(@PathVariable("name") String Tenantname){
        userService.deleteUserByname(Tenantname);
        String personID=Tenantname;
        return new ResponseEntity<>(personID, HttpStatus.CREATED);
    }

    @DeleteMapping(value = "deleteuserid/{id}")
    public ResponseEntity<String> deleteUserById(@PathVariable("id") UUID TenantID){
        userService.deleteUserById(TenantID);
        UUID personID=TenantID;
        return new ResponseEntity(personID, HttpStatus.CREATED);
    }




@RestController

  @CrossOrigin()

  @RequestMapping(value="/property")

  public class PropertyController {

    private final PropertyService propertyService;
    private final UserService userService;

    @Autowired
    public PropertyController(PropertyService propertyService,UserService userService) {
        this.propertyService = propertyService;
        this.userService = userService;
    }


    @PostMapping()
    public ResponseEntity<UUID> insertProperty(@Valid @RequestBody PropertyDTO PropertyDTO) {
        UUID personID = propertyService.insert(PropertyDTO);
        return new ResponseEntity<>(personID, HttpStatus.CREATED);
    }

    @PutMapping()
    public ResponseEntity<UUID> insertintoProperty(@Valid @RequestBody PropertyDTO PropertyDTO) {
        UUID personID = propertyService.insert(PropertyDTO);
        return new ResponseEntity<>(personID, HttpStatus.CREATED);
    }

    @GetMapping()
    public ResponseEntity<List<PropertyDTO>> getProperty() {

        List<PropertyDTO> dtos = propertyService.findProperty();

        for (PropertyDTO dto : dtos) {

            Link personLink = linkTo(methodOn(PropertyController.class)

                    .getProperty(dto.getId())).withRel("personDetails");

            dto.add(personLink);
        }
        return new ResponseEntity<>(dtos, HttpStatus.OK);
    }
    
    @GetMapping(value = "/{id}")
    public ResponseEntity<PropertyDTO> getProperty(@PathVariable("id") UUID PropertyID) {
        PropertyDTO dto = propertyService.findPropertyById(PropertyID);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @GetMapping(value = "propertname/{name}")
    public ResponseEntity<PropertyDTO> getPropertyByName(@PathVariable("name") String   PropertyName) {
        PropertyDTO dto = propertyService.findPropertyByName(PropertyName);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @GetMapping(value = "propertowner/{name}")
    public ResponseEntity<PropertyDTO> getPropertyByUser(@PathVariable("name") String UserName) {
        UserDTO dto = userService.findUserByName(UserName);
        Userinfo user= UserBuilder.toEntity(dto);
        PropertyDTO prdto = propertyService.findPropertyByUser(user);
        return new ResponseEntity<>(prdto, HttpStatus.OK);
    }


    @GetMapping(value = "propertylocation/{location}")
    // @RequestMapping(method=RequestMethod.GET)
    public ResponseEntity<PropertyDTO> getPropertyByLocation(@PathVariable("location") String PropertyLocation) {
        PropertyDTO dto = propertyService.findPropertyByLocation(PropertyLocation);
        return new ResponseEntity<>(dto, HttpStatus.OK);
    }

    @DeleteMapping(value = "/{name}")
    public ResponseEntity<String> deleteByname(@PathVariable("name") String Propertyname){
        propertyService.deletePropertyByname(Propertyname);
        String personID=Propertyname;
        return new ResponseEntity<>(personID, HttpStatus.CREATED);
    }

如果我尝试在 Postman 中使用以下任何路径:

http://localhost:8080/userinfo

http://localhost:8080//属性

返回 404 not found 错误。

如果打开http://localhost:8080,显示如下:

用户信息
href "http://localhost:8080/userinfoes{?page,size,sort}" 模板化的真 预订
href "http://localhost:8080/bookings{?page,size,sort}" 模板化的真 属性
href "http://localhost:8080/properties{?page,size,sort}" 模板化的真 轮廓 href "http://localhost:8080/profile"

我可以将上面的地址仅用于基本的帖子,并且可以不带参数获取。如果添加参数,则会显示如下错误:

“原因”:{ “原因”:空, “消息”:“无效的 UUID 字符串:名称” }, “消息”:“无法将类型 [java.lang.String] 转换为类型 [java.util.UUID] 的值 'name';嵌套异常是 java.lang.IllegalArgumentException:无效的 UUID 字符串:名称” }

pom 看起来像这样:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.发布 com.rentBackEnd 演示 0.0.1-快照 后端 最终项目的后端 罐子

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- TESTING -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

    </plugins>
</build>

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <configLocation>checkstyle.xml</configLocation>
            </configuration>
        </plugin>
    </plugins>
</reporting>

【问题讨论】:

    标签: java spring spring-boot crud


    【解决方案1】:

    除了@VeKe 回答的内容之外,您还在控制器级别映射了@RequestMapping(value="/userinfo")。所以你所有的底层 URI 都将变成(类似于)如下:

    /userinfo/userid/{foo}
    /userinfo/userrole/{bar}
    /userinfo/userinfoname/{blahblah}
    

    尝试点击正确的 URI,你应该会很好。

    【讨论】:

      【解决方案2】:

      检查你的控制器路径在邮递员userinfo,但在日志中有http://localhost:8080/userinfoes以es`结尾

      并且对于路径变量更改类型为String

      【讨论】:

        【解决方案3】:
        @GetMapping(value = "userid/{id}")
            public ResponseEntity<UserDTO> getUserByID(@PathVariable("id") UUID TenantID) {
                UserDTO dto = userService.findUserById(TenantID);
                return new ResponseEntity<>(dto, HttpStatus.OK);
            }
        

        您的输入必须是有效的UUID。 (因为 UUID TenantID)

        用户ID/62fe0290-3702-4b4b-bf02-42103db90b0f

        类似的东西

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-23
          • 2015-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-10
          相关资源
          最近更新 更多