我目前正在使用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)