I am having a problem with retrieving results using the full value of strings being passed to a REST endpoint.
If I pass the full 6 character string for the value "communityNo", I get an empty Json array back. If I pass 1-3 of the first 6 characters for communityNo, then I get tons of records back.
REST endpt url: http://localhost:8080/web-portal/api/aw501?stateId=2&communityNo=200
criteria api specifications
public static Specification<Aw501> findAllByStateId(String stateId){
return new Specification <Aw501>(){
public Predicate toPredicate(Root<Aw501> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.like(root.get(Aw501_.stateId), stateId + "%"); //MDB
}
} ;
}
public static Specification<Aw501> findAllByCommunityNo(String communityNo){
return new Specification <Aw501>(){
public Predicate toPredicate(Root<Aw501> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.like(root.get(Aw501_.communityNo), communityNo + "%"); //MDB
}
} ;
}
}
package pivot.fema.dhs.gov.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import pivot.fema.dhs.gov.domain.report.Aw501;
import java.util.List;
/**
* Spring Data JPA repository for the Claims entity.
*/
@Repository
public interface Aw501Repository
extends JpaRepository<Aw501, Long>, JpaSpecificationExecutor<Aw501> {
}
@Service
@Transactional
public class Aw501Service {
private final Logger log = LoggerFactory.getLogger(Aw501Service.class);
private final Aw501Repository aw501Repository;
private final CacheManager cacheManager;
private ReportSessionParameters reportSessionParameters;
@Inject
private ApplicationProperties applicationProperties;
private ApplicationProperties.Performance performance;
public Aw501Service(Aw501Repository aw501Repository,
CacheManager cacheManager){
this.aw501Repository = aw501Repository;
this.cacheManager = cacheManager;
}
@Transactional(readOnly = true)
public Page<Aw501DTO> getAw501Reports(Integer pageNumber,
Integer pageSize,
String sortBy,
String sortOrder,
String stateId,
String communityNo){
boolean flagStateId = StringUtils.isNotBlank(stateId) ;
boolean flagCommunityNo = StringUtils.isNotBlank(communityNo) ;
if(flagStateId){
log.info("****Aw501Service.java flag input for flagStateId :" + true + "****");
}else{
}
if(flagCommunityNo){
log.info("****Aw501Service.java flag input for flagCommunityNo :" + true + "****");
}else{
}
Specifications<Aw501> predicates = null ;
if(flagStateId && flagCommunityNo){
predicates = Specifications.where(Aw501Spec.findAllByStateId(stateId)) ;
predicates = predicates.and(Specifications.where(Aw501Spec.findAllByCommunityNo(communityNo)));
}
if( flagStateId && flagCommunityNo){
if(flagStateId){
log.info("****Aw501Service.java (flagStateId && flagCommunityNo):" + true + "**** predicates = " + predicates.toString());
}else{
}
}
if(flagStateId && !flagCommunityNo){
predicates = Specifications.where(Aw501Spec.findAllByStateId(stateId)) ;
}
if(flagStateId && !flagCommunityNo){
log.info("****Aw501Service.java (flagStateId && !flagCommunityNo):" + true + "**** predicates = " + predicates.toString());
}else{
}
if( !flagStateId && flagCommunityNo){
predicates = Specifications.where(Aw501Spec.findAllByCommunityNo(communityNo)) ;
}
if(!flagStateId && flagCommunityNo ){
log.info("****Aw501Service.java (!flagStateId && flagCommunityNo):" + true + "**** predicates = " + predicates.toString());
}else{
}
PageRequest pageRequest = new PageRequest(1,25);
if(pageRequest != null ){
log.info("****Aw501Service.java (pageRequest): pageRequest is not null" + "**** pageNumber = " + pageRequest.toString() + " pageOffset : " + "" );
}else{
log.info("****Aw501Service.java (pageRequest): pageRequest is null" + "**** " );
}
Page<Aw501DTO> foobar = aw501Repository
.findAll(predicates,pageRequest)
.map(Aw501DTO::new);
if(foobar != null ){
log.info("****Aw501Service.java (foobar): foobaris not null" + "**** foobar = " + foobar.hasContent() + " pageOffset : " + "" );
}else{
log.info("****Aw501Service.java (foobar): pageRequest is null" + "**** " );
}
return foobar ;
}//end getAw501Reports
}
@RestController
@RequestMapping("/api")
public class Aw501Resource{
Page<Aw501DTO> page = new PageImpl<Aw501DTO>(Collections.<Aw501DTO>emptyList());
private final Logger log = LoggerFactory.getLogger(Aw501Resource.class);
private final Aw501Repository aw501Repository;
private final Aw501Service aw501Service;
private final ReportSessionParameters reportSessionParameters;
public Aw501Resource(Aw501Repository aw501Repository,
Aw501Service aw501Service,
ReportSessionParameters reportSessionParameters) {
this.aw501Repository = aw501Repository;
this.aw501Service = aw501Service;
this.reportSessionParameters = reportSessionParameters;
}
/*
A page is a sublist of a list of objects. It allows gain
information about the about the posistion of it in
th e containing entire list.
*/
@GetMapping("/aw501")
public Page<Aw501DTO> getAw501(@RequestParam(name = "stateId", required = false) String stateId,
@RequestParam(name = "communityNo", required = false) String communityNo,
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "sortOrder", required = false) String sortOrder,
@RequestParam(name = "pageSize", required = false) Integer pageSize,
@RequestParam(name = "pageNumber", required = false) Integer pageNumber){
if (StringUtils.isNotBlank(stateId)){
log.info("****Aw501Resource.java input for stateId :" + stateId + "****");
stateId = stateId.trim() ;
}
if (StringUtils.isNotBlank(communityNo)){
log.info("****Aw501Resource.java input for communityNo :" + communityNo + "****");
communityNo = communityNo.trim() ;
}
if (StringUtils.isNotBlank(sortBy)){
log.info("****Aw501Resource.java input for sortBy :" + sortBy + "****");
sortBy = sortBy.trim() ;
}
if (StringUtils.isNotBlank(sortOrder)){
log.info("****Aw501Resource.java input for sortOrder :" + sortOrder + "****");
sortOrder = sortOrder.trim() ;
}
if (pageNumber != null && pageNumber.compareTo(0) > 0){
log.info("****Aw501Resource.java input for pageNumber :" + pageNumber.toString() + "****");
pageNumber-- ;
log.info("****Aw501Resource.java input for pageNumber-- :" + pageNumber.toString() + "****");
}
return aw501Service.getAw501Reports(pageNumber, pageSize, sortBy, sortOrder, stateId,communityNo);
}
@GetMapping("/foobar")
public Map<String,String> foobar(@RequestParam(name = "stateId", required = false) String stateId,
@RequestParam(name = "communityNo", required = false) String communityNo,
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "sortOrder", required = false) String sortOrder,
@RequestParam(name = "pageSize", required = false) Integer pageSize,
@RequestParam(name = "pageNumber", required = false) Integer pageNumber){
Map<String , String> map = new HashMap<String,String>() ;
map.put("stateId","51") ;
map.put("sortBy","DESC");
map.put("pageSize","25");
map.put("pageNumber","100");
return map ;
}
}
Any help you guys could provide would be extremely appreciated!!!! Thanks!!!
The problem was in Aw501Service.java
PageRequest pageRequest = new PageRequest(1,25);
The reason why I was getting results with only partial inputs that matched the predicate wild card in the criteria code but nothing when the input was an exact match; was that the PageRequest object uses an array internally to store retrieved objects. If there is only one object returned...as would be expected from an exact match within the context of my application....then accessing that array at index [1] will naturally return "nothing". I correct the issue via the simple modification to the aforementioned line of code to:
PageRequest pageRequest = new PageRequest(0,25);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.