I have an API. How to return the values of only one column (name) from the database? Request code in the controller:
@RestController
public class PURController {
@Autowired
private PURRepository purRepository;
@CrossOrigin()
@GetMapping("/purs/getname/{device}")
public ResponseEntity<ArrayList<PUR>> getPurName(@PathVariable(value = "device") String device){
ArrayList<PUR> pur = purRepository.findAllByDevice(device);
return ResponseEntity.ok().body(pur);
}
In this case, all rows found by the "device" field are returned. Also I have a method in PUR.java:
public String getName() {
return name;
}
How can it all be connected?
Also: my repository class:
@Repository
public interface PURRepository extends JpaRepository<PUR, Long> {
ArrayList<PUR> findAllByDevice(String device);
Optional<PUR> findByDevice(String device);}
Response now:
[{"id":48,"name":"6-PRN.00-00.01","quantity":3,"device":"6-PRN.01-01.000"},{"id":49,"name":"6-PRN.00-00.02","quantity":7,"device":"6-PRN.01-01.000"},{"id":50,"name":"6-PRN.00-00.03","quantity":4,"device":"6-PRN.01-01.000"}]
Response i want:
[{6-PRN.00-00.01},{6-PRN.00-00.02},{6-PRN.00-00.03}]
You don't need to use stream. You can solve it easily:
@Query(value = "SELECT p.name FROM PUR p where p.device= :device", nativeQuery = true)
List<String> findAllNamesByDevice(@Param("device") String device);