Java 如何以不区分大小写的方式使用SpringRESTAPI检索数据?
我正在用Spring Boot制作一个RESTAPI。 我的控制器中有这样一种方法:Java 如何以不区分大小写的方式使用SpringRESTAPI检索数据?,java,spring,api,rest,spring-data-jpa,Java,Spring,Api,Rest,Spring Data Jpa,我正在用Spring Boot制作一个RESTAPI。 我的控制器中有这样一种方法: @GetMapping(“/dead”) @应答器 public ResponseEntity getDeadByCountryRegion(@RequestParam(value=“country”)字符串countryRegion){ 列表实体=serviceDead.FindentialByCountryRegion(countryRegion); 返回新的响应属性(实体,HttpStatus.OK);
@GetMapping(“/dead”)
@应答器
public ResponseEntity getDeadByCountryRegion(@RequestParam(value=“country”)字符串countryRegion){
列表实体=serviceDead.FindentialByCountryRegion(countryRegion);
返回新的响应属性(实体,HttpStatus.OK);
}
我的url看起来像:http://localhost:8080/api/v1/dead/?country=俄罗斯
但是我也希望能够通过这个url获取数据。http://localhost:8080/api/v1/dead/?country=俄罗斯
我该怎么办?谢谢
实体
公共类DeadEntity实现可序列化{
@身份证
私人长id;
私有字符串provinceState;
私人区域;
私有字符串lat;
私有字符串lon;
私人案件;
前一天的私人int;
公共实体(){
}
公共死实体(长id、字符串provinceState、字符串countryRegion、字符串lat、字符串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;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
public int getLatestTotalCases(){
返回最新的总病例;
}
public void setLatestTotalCases(int latestTotalCases){
this.latestTotalCases=latestTotalCases;
}
public int getDiffFromPrevDay(){
从前一天返回;
}
公共无效设置diffFromPrevDay(int diffFromPrevDay){
this.diffFromPrevDay=diffFromPrevDay;
}
公共字符串getProvinceState(){
返回provinceState;
}
public void setProvinceState(字符串provinceState){
this.provinceState=provinceState;
}
公共字符串getCountryRegion(){
返回农村地区;
}
公共void setCountryRegion(字符串countryRegion){
this.countryRegion=countryRegion;
}
公共字符串getLat(){
返回lat;
}
公共void setLat(字符串lat){
this.lat=lat;
}
@JsonProperty(“长”)
公共字符串getLon(){
返回离子;
}
公共无效设置(字符串设置){
this.lon=lon;
}
}
我从CSV远程文件获取数据
private void fillentityproperty(Iterable newRecord,List newEntity){
用于(CSVRecord记录:新记录){
DeadEntity locationStats=新的DeadEntity();
对于(长j=0;j)查询参数,问题不在于url
@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);
}
@GetMapping(“/dead”)
@应答器
public ResponseEntity getDeadByCountryRegion(@RequestParam(value=“country”)字符串countryRegion){
列表实体=serviceDead.FindentialByCountryRegion(countryRegion);
返回新的响应属性(实体,HttpStatus.OK);
}
我的url看起来像:
http://localhost:8080/api/v1/dead/?country=Russia
但是我也希望能够通过这个url获取数据。
http://localhost:8080/api/v1/dead/?country=russia
如果您想将其作为url,这将是一个问题http://localhost:8080/api/v1/dead/?国家=俄罗斯
因此,@RequestParam并不关心查询参数的值
然而,这可以在数据库级别上处理,取决于您如何进行查询,您可以采取一些措施使该案例具有说服力
在JPA存储库中声明一个名为findByCountryRegionIgnoreCase(countryRegion);
而不是findByCountryRegion(countryRegion)
然后在您执行serviceDead.findEntityByCountryRegion(countryRegion);
编辑:更正了存储库方法名称更改参数会有什么帮助?您需要发出不同的查询。FindentityByCountryRegionInoRecase
应该是dao上您的方法名称。哇,非常感谢。我甚至不知道Spring Jpa提供了这样的功能。我是Spring的新手:))15天前,我开始了。亲爱的Boug,非常感谢你的指导。正是我想要的。@ FaramarzAfzali,如果解决了你的问题,请考虑接受答案。