【发布时间】:2018-02-25 15:15:57
【问题描述】:
我是 Spring Boot 开发的新手,在某些情况下我需要一些帮助。
我正在使用 JPA/Hibernate Spring 和 CRUDrepository 创建一个 RESTful 网络服务。(Json 格式)
我有以下表格轨道:
+----+----------+-----------+
| id | filename |
+----+----------+-----------+
| 1 | public/11111/Cool |
| 2 | public/22222/Cool |
| 3 | public/33333/Cool |
| 4 | public/44444/Cool |
| 5 | public/55555/Cool |
| 6 | public/66666/Cool |
| 7 | private/77777/Cool |
| 8 | private/88888/Cool |
| 9 | private/99999/Cool |
+----+----------+-----------+
我有以下实体:
@Entity
public class Track{
@Id
@Column(name="id")
private int id;
@Column(name="filename")
private String filename;
public Track() {
super();
}
public Track(int id, String filename) {
super();
this.id= id;
this.filename= filename;
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public String getfilename() {
return dateiName;
}
public void setfilename(String filename) {
this.filename = filename;
}
}
CRUD:
public interface TrackRepository extends CrudRepository<Track, Integer> {
List<Track> findByid(int id);
}
控制器:
@Controller
@RequestMapping(path="rest/music")
public class TrackController {
@Autowired
private TrackRepository trackRepository;
@GetMapping(path="/all")
public @ResponseBody Iterable<Track> getAllTrack() {
return trackRepository.findAll();
}
@GetMapping(path="/id")
public @ResponseBody Iterable<Track>
getTrackById(@RequestParam("id") int id){
return trackRepository.findByid(id);
}
@GetMapping(path="/public")
public @ResponseBody Iterable<Track>
getPublicTrack(){
return ...working
}
@GetMapping(path="/private")
public @ResponseBody Iterable<Track>
getPrivateTrack(){
return ...working
}
}
所以我可以获取所有 Track 或通过 id 搜索它们。
现在我的问题来了,我如何才能返回电影名称以“public”或“private”开头的所有音轨。
所以我得到一个像这样的 json 响应:
私人:
[
{
"id": 7,
"filename": "private/77777/Cool"
}
{
"id": 8,
"filename": "private/88888/Cool"
}
{
"id": 9,
"filename": "private/99999/Cool"
}
]
我有点需要过滤文件名字符串,然后......我无法得到解决方案。
【问题讨论】:
标签: java spring hibernate spring-mvc jpa