首页 > 解决方案 > Spring Boot 不会创建在 @RequestMapping(value="/path") 中定义的路径

问题描述

我的 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,会显示如下:

userinfoes
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"

我可以将上述地址仅用于基本帖子并无需参数即可获取。如果添加参数,则会显示以下错误:

"cause": { "cause": null, "message": "Invalid UUID string: name" }, "message": "从 [java.lang.String] 类型转换为 [java.util.UUID] 类型失败对于值'name';嵌套异常是 java.lang.IllegalArgumentException: Invalid UUID string:name" }

pom 看起来像这样:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.3.RELEASE com.rentBackEnd demo 0.0.1-SNAPSHOT backEnd backEnd for final project jar

<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>

标签: javaspringspring-bootcrud

解决方案


@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)

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

像这样的东西


推荐阅读