简体   繁体   English

Spring CrudRepository,Web服务,使用某些String返回json对象

[英]Spring CrudRepository, Webservice, return json object with certain String

I am pretty new to Spring Boot development and i need some help with a certain case. 我是Spring Boot开发的新手,在某些情况下我需要一些帮助。

I am creating a RESTful webservices with the JPA/Hibernate Spring and the CRUDrepository.(Json format) 我正在使用JPA / Hibernate Spring和CRUDrepository创建RESTful Web服务。(Json格式)

I have the following Table Track: 我有以下表格跟踪:

    +----+----------+-----------+
    | 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 |
    +----+----------+-----------+

I have the Following Entity: 我有以下实体:

@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;
    }
}

The CRUD: CRUD:

public interface TrackRepository extends CrudRepository<Track, Integer> {

    List<Track> findByid(int id);

}

The Controller: 控制器:

@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

    }

}

So i can get all Track or search them by id. 这样我就可以获取所有Track或按ID搜索它们。

Now comes my question how can i return all Track where the filme name starts with "public" or "private". 现在出现了我的问题,我该如何返回电影名以“ public”或“ private”开头的所有Track。

So i get a json response like: 所以我得到一个像json的响应:

private: 私人的:

[
       {
            "id": 7,
            "filename": "private/77777/Cool"
       }
       {
            "id": 8,
            "filename": "private/88888/Cool"
       }
       {
            "id": 9,
            "filename": "private/99999/Cool"
       }
]

I kinda need to filter the filename string and then... i just cant get the solution. 我有点需要过滤文件名字符串,然后...我只是无法解决问题。

you can do this using hql 你可以使用hql做到这一点

  public interface MusicRepository extends CrudRepository<Music, Integer> {

List<Music> findByid(int id);

@Query("Select m from Music m where m.filename like '%private' or m.filename like '%public'")
List<Music> findCustom();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring CrudRepository 在使用 Lombok @Data Annotation 时无法从有效的 object 返回 JSON - Spring CrudRepository failing to return JSON from valid object while using Lombok @Data Annotation Spring Boot:在某些类中,Autowired CRUDRepository为空 - Spring Boot: Autowired CRUDRepository null, in certain classes Spring data CrudRepository (hibernate) findone return null - Spring data CrudRepository (hibernate) findone return null Spring CrudRepository 按结果返回组为 Map - Spring CrudRepository return group by result as a Map Spring RESTful Web服务Hibernate / JPA Crudrepository-通过电影标题获取用户 - Spring RESTful webservice Hibernate/JPA Crudrepository - Get User by Movie title Spring Boot CrudRepository查询传递对象mysql - spring boot CrudRepository query passing object mysql Spring RESTful Webservice - 返回没有模型对象的 JSON - Spring RESTful Webservice - Returning JSON without model object 使用Spring获取RESTful Web服务以了解JSON字符串 - Getting a RESTful webservice with Spring to understand a JSON-string 将单个 Json object 作为字符串返回 - Return a Single Json object as a String Spring Data CrudRepository 方法通过object嵌套在object保存在数组中查找 - Spring Data CrudRepository Method to search by object nested in object saved in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM