相关疑难解决方法(0)

春季启动.如何将可选<>传递给实体类

我目前正在使用spring创建一个网站,我偶然发现了这个基本场景,我对如何解决这个特定代码一无所知:Entity = Optional;

RoomEntity roomEntity =  roomRepository.findById(roomId);
Run Code Online (Sandbox Code Playgroud)

ReservationResource(API请求类):

    public class ReservationResource {
    @Autowired
    RoomRepository roomRepository;

    @RequestMapping(path = "/{roomId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<RoomEntity> getRoomById(
    @PathVariable
    Long roomId){
        RoomEntity roomEntity =  roomRepository.findById(roomId);
        return new ResponseEntity<>(roomEntity, HttpStatus.OK);}
    }}
Run Code Online (Sandbox Code Playgroud)

RoomRepository类:

public interface RoomRepository extends CrudRepository<RoomEntity, Long> {
    List<RoomEntity> findAllById(Long id);
}
Run Code Online (Sandbox Code Playgroud)

RoomEntity

@Entity
@Table(name = "Room")
public class RoomEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    private Integer roomNumber;

    @NotNull
    private String price;

    public RoomEntity() {
        super();
    } …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

6
推荐指数
3
解决办法
9111
查看次数

标签 统计

java ×1

spring-boot ×1