获取所有项的映射方法返回一个空列表

时间:2018-05-21 14:14:17

标签: java spring hibernate spring-boot

我正在通过this Lynda tutorial学习Spring Boot。

通过GET方法/room我应该能够获得嵌入式数据库中所有当前房间的列表。 schema.sqldata.sql文件都已到位。

我的实体类Room.java

package com.frankmoley.landon.data.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "ROOM")
public class Room {
    @Id
    @Column(name = "ROOM_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "ROOM_NUMBER")
    private String number;
    @Column(name = "BED_INFO")
    private String bedInfo;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getBedInfo() {
        return bedInfo;
    }

    public void setBedInfo(String bedInfo) {
        this.bedInfo = bedInfo;
    }

}

存储库类RoomRepository.java

package com.frankmoley.landon.data.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.frankmoley.landon.data.entity.Room;

@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {

    Room findByNumber(String number);
}

这是我们创建的临时控制器类,只是为了测试这个方法(RoomController.java):

package com.frankmoley.landon.data.webservice;

import com.frankmoley.landon.data.entity.Room;
import com.frankmoley.landon.data.repository.RoomRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class RoomController {
    @Autowired
    private RoomRepository repository;

    @RequestMapping(value="/rooms", method= RequestMethod.GET)
    List<Room> findAll(@RequestParam(required=false) String roomNumber){
       List<Room> rooms = new ArrayList<>();
        if(null==roomNumber){
            Iterable<Room> results = this.repository.findAll();
            results.forEach(room-> {rooms.add(room);});
        }else{
            Room room = this.repository.findByNumber(roomNumber);
            if(null!=room) {
                rooms.add(room);
            }
        }
        return rooms;
    }
}

当我运行应用程序时,我在Eclipse中看到有关此映射的消息:

  

映射&#34; {[/ rooms],methods = [GET]}&#34;到   java.util.List中   com.frankmoley.landon.data.webservice.RoomController.findAll(java.lang.String中)

但是当我导航到localhost:8080/rooms时,我得到的只是一个空的JSON对象。

application.properties:

spring.jpa.hibernate.dll-auto=none
management.endpoints.web.exposure.include=*

1 个答案:

答案 0 :(得分:1)

尝试在ddl-auto上设置更新,然后创建创建 - 删除

spring.jpa.hibernate.ddl-auto=update