使用Spring JPA和REST显示数据

时间:2016-02-01 17:47:57

标签: spring rest spring-mvc spring-data-jpa spring-rest

按照这些教程12,我成功地将RESTful Spring应用程序与JPA结合在一起。目前,我的驱动程序存储库中有3个驱动程序。

我的问题是当我使用localhost:8080 /驱动程序时,它说:

{
  "_embedded" : {
"driver" : [ {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/1"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/1"
    }
  }
}, {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/2"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/2"
    }
  }
}, {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/3"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/3"
    }
  }
} ]
  },
  "_links" : {
    "self" : {
  "href" : "http://localhost:8080/driver"
   },
    "profile" : {
  "href" : "http://localhost:8080/profile/driver"
   },
   "search" : {
    "href" : "http://localhost:8080/driver/search"
}
  },
 "page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
  }
}

当我进入特定的驱动程序页面时,就像localhost:8080 / driver / 1,它说:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/1"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/1"
    }
  }
}

在我的驱动程序实体类中,我有firstName,lastName,phone等字段。我的问题是:有没有办法可以显示localhost:8080 / driver或localhost:8080 / driver / 1?所以它看起来类似于:

 {
  "firstName" : "Frodo",
  "lastName" : "Baggins",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people/1"
    }
   }
}

我知道字段已正确保存在数据库中,因为我可以成功搜索它们,但我还没有找到如何显示它们的示例,除了使用curl进行POST。

提前感谢任何可以提供帮助的人!

编辑:这是我的初学者课程:

@SpringBootApplication
 public class StartServer implements    ApplicationListener<ContextRefreshedEvent> {


private static final Logger log = LoggerFactory.getLogger(StartServer.class);


public static void main(String[] args) {
    SpringApplication.run(StartServer.class, args);

}

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {

}

@Bean
public CommandLineRunner demo(DriverRepository repository) {
    return (args) -> {
        repository.save(new Driver("Jack", "Bauer"));
        repository.save(new Driver("JackY", "Aasd"));
        repository.save(new Driver("JackMe", "Commou"));

        // fetch all customers
        log.info("Drivers found with findAll():");
        log.info("-------------------------------");
        for (Driver driver : repository.findAll()) {
            log.info(driver.toString());
        }
        log.info("");

        Driver driver = repository.findOne(1L);
        log.info("Customer found with findOne(1L):");
        log.info("--------------------------------");
        log.info(driver.toString());
        log.info("");

        // fetch customers by last name
        log.info("Driver found with findByLastName('Bauer'):");
        log.info("--------------------------------------------");
        for (Driver bauer : repository.findByLastName("Bauer")) {
            log.info(bauer.toString());
        }
        log.info("");
    };
}



}

我的DriverRepository类:

@RepositoryRestResource(collectionResourceRel = "driver", path = "driver")

public interface DriverRepository extends PagingAndSortingRepository<Driver, Long> {

List<Driver> findByLastName(@Param("name") String name);
}

我没有/ driver的Controller类,因为@RepositoryRestResource(collectionResourceRel =&#34; driver&#34;,path =&#34; driver&#34;)为我处理了它。

我的驱动程序实体类:

@Entity
public class Driver {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
String firstName;
String lastName;

protected Driver() {

}

public Driver(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

@Override
public String toString() {
    return String.format( "Customer[id=%d, firstName='%s', lastName='%s']",
            id, firstName, lastName);
}


}

0 个答案:

没有答案