Reputation: 795
I am making a Rest API with Spring Boot. I have a method like this in my controller:
@GetMapping("/dead")
@ResponseBody
public ResponseEntity<List<DeadEntity>> getDeadByCountryRegion(@RequestParam(value = "country") String countryRegion) {
List<DeadEntity> entity = serviceDead.findEntityByCountryRegion(countryRegion);
return new ResponseEntity<>(entity, HttpStatus.OK);
}
and my url looks like: http://localhost:8080/api/v1/dead/?country=Russia
but I want to be able get data with this url too. http://localhost:8080/api/v1/dead/?country=russia
What should I do? Thanks.
Entity
public class DeadEntity implements Serializable {
@Id
private Long id;
private String provinceState;
private String countryRegion;
private String lat;
private String lon;
private int latestTotalCases;
private int diffFromPrevDay;
public DeadEntity() {
}
public DeadEntity(Long id, String provinceState, String countryRegion, String lat, String lon, int latestTotalCases, int diffFromPrevDay ) {
this.id = id;
this.provinceState = provinceState;
this.countryRegion = countryRegion;
this.lat = lat;
this.lon = lon;
this.latestTotalCases = latestTotalCases;
this.diffFromPrevDay = diffFromPrevDay;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getLatestTotalCases() {
return latestTotalCases;
}
public void setLatestTotalCases(int latestTotalCases) {
this.latestTotalCases = latestTotalCases;
}
public int getDiffFromPrevDay() {
return diffFromPrevDay;
}
public void setDiffFromPrevDay(int diffFromPrevDay) {
this.diffFromPrevDay = diffFromPrevDay;
}
public String getProvinceState() {
return provinceState;
}
public void setProvinceState(String provinceState) {
this.provinceState = provinceState;
}
public String getCountryRegion() {
return countryRegion;
}
public void setCountryRegion(String countryRegion) {
this.countryRegion = countryRegion;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
@JsonProperty("long")
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
}
I get data from CSV remote file
private void fillEntityProperties(Iterable<CSVRecord> newRecord, List<DeadEntity> newEntity) {
for (CSVRecord record : newRecord) {
DeadEntity locationStats = new DeadEntity();
for (long j = 0; j <= newEntity.size(); j++)
locationStats.setId(j);
locationStats.setProvinceState(record.get("Province/State"));
locationStats.setCountryRegion(record.get("Country/Region")); //The country name comes from remote data source in Uppercase mode
locationStats.setLat(record.get("Lat"));
locationStats.setLon(record.get("Long"));
int latestCases = Integer.parseInt(record.get(record.size() - 1));
int prevDayCases = Integer.parseInt(record.get(record.size() - 2));
locationStats.setLatestTotalCases(latestCases);
locationStats.setDiffFromPrevDay(latestCases - prevDayCases);
newEntity.add(locationStats);
saveDeadInDB(locationStats);
}
}
Upvotes: 1
Views: 402
Reputation: 19173
For query parameters it is not your url the problem
@GetMapping("/dead")
@ResponseBody
public ResponseEntity<List<DeadEntity>> getDeadByCountryRegion(@RequestParam(value = "country") String countryRegion) {
List<DeadEntity> entity = serviceDead.findEntityByCountryRegion(countryRegion);
return new ResponseEntity<>(entity, HttpStatus.OK);
}
and my url looks like: http://localhost:8080/api/v1/dead/?country=Russia
but I want to be able get data with this url too. http://localhost:8080/api/v1/dead/?country=russia
It would be a problem if you wanted as url the http://localhost:8080/api/v1/dead/?COUNTRY=russia
So the value of the query parameter is not something the @RequestParam cares about.
However that could be handled on your DB level on how you make your queries, and there is something that you can do to make that case incensitive.
Declare in your JPA repository a method with name findByCountryRegionIgnoreCase(countryRegion);
instead of findByCountryRegion(countryRegion)
and fire that on your controller where you execute the serviceDead.findEntityByCountryRegion(countryRegion);
Edit: Corrected repository method names
Upvotes: 1