Here is my entity class:
package com.test.model; import java.sql.Timestamp; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import com.fasterxml.jackson.annotation.JsonFormat; @Entity @Table(name = "Ergebnisse") public class Ergebnisse{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "messwert") private double Messwert; @Column(name = "einheit") private String Einheit; @Column(name = "material") private String Material; @Column(name = "laboreingangsdatum") @JsonFormat(pattern="yyyy-MM-dd") private Timestamp Laboreingangsdatum; @Column(name = "status") private String Status; @Column(name = "parameter") private String Parameter; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getMesswert() { return Messwert; } public void setMesswert(double messwert) { Messwert = messwert; } public String getEinheit() { return Einheit; } public void setEinheit(String einheit) { Einheit = einheit; } public String getMaterial() { return Material; } public void setMaterial(String material) { Material = material; } public Timestamp getLaboreingangsdatum() { return Laboreingangsdatum; } public void setLaboreingangsdatum(Timestamp laboreingangsdatum) { Laboreingangsdatum = laboreingangsdatum; } public String getStatus() { return Status; } public void setStatus(String status) { Status = status; } public String getParameter() { return Parameter; } public void setParameter(String parameter) { Parameter = parameter; } }
Here is my Repository:
package com.test.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.test.model.Ergebnisse; @Repository public interface ErgebnisseRepository extends JpaRepository<Ergebnisse,Integer> { @Query("FROM Ergebnisse where Material=:Material") List<Ergebnisse> findMaterial(@Param("Material") String Material); }
And my controller:
package com.test.controllers; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.test.load.ErgebnisseRequest; import com.test.model.Ergebnisse; import com.test.repository.ErgebnisseRepository; @RestController @CrossOrigin(origins = "*") @RequestMapping("/api") public class ErgebnisseController { @Autowired ErgebnisseRepository repository; @RequestMapping(value="/ergebnisse", method=RequestMethod.GET) public List<Ergebnisse> getData(ErgebnisseRequest request){ List<Ergebnisse> ergebnisseList = repository.findAll(); return ergebnisseList; } @RequestMapping(value="/search", method=RequestMethod.GET) public List<Ergebnisse> Search(ErgebnisseRequest request){ List<Ergebnisse> ergebnisseList = repository.findMaterial(request.getMaterial()); return ergebnisseList; }
Here is my Database: My Database
Edit: Here is my ErgebnisseRequest class:
package com.test.load; import java.sql.Timestamp; import javax.persistence.Column; import com.fasterxml.jackson.annotation.JsonFormat; public class ErgebnisseRequest { @Column(name="material") private String material; public String getMaterial() { return material; } public void setMaterial(String material) { this.material = material; } }
So the repository.findAll() gives me all the data from the database. But when i am trying to search by Material it gives me a emptylist and no error. I try all of it like changing the uppercase letters to lowercase etc.. and help would be appreciated.
Here is the photo of it in Postman: find by Material test
Advertisement
Answer
Try changing your HTTP request from GET
to POST
in order to be able to consume the RequestBody
content you are trying to send.
e.g.
@RequestMapping(value="/ergebnisse", method=RequestMethod.POST) public List<Ergebnisse> getData(@RequestBody ErgebnisseRequest request)
and change your postman request from GET to POST
OR
you can still use GET
but pass the content as request parameter
e.g.
@RequestMapping(value="/ergebnisse", method=RequestMethod.GET) public List<Ergebnisse> getData(@RequestParam(name = "material") String material) { List<Ergebnisse> ergebnisseList = repository.findMaterial(material); return ergebnisseList; }
and in your postman maintain the GET request but modify the URL to:
http://localhost:9090/test/api/search?material=Germini