TheWandererr
TheWandererr

Reputation: 514

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.

I am creating a RESTful webservices with the JPA/Hibernate Spring and the CRUDrepository.(Json format)

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:

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.

Now comes my question how can i return all Track where the filme name starts with "public" or "private".

So i get a json response like:

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.

Upvotes: 1

Views: 3207

Answers (1)

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

you can do this using 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();
}

Upvotes: 2

Related Questions