Spring boot API call with multiple @RequestParam
Your problem is very simple - in your call
$ curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01&end=2018-10-15
&
sign tell operation system 'run curl -X GET http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01
in background'. This is a reason why end
date is undefined.
Just surround your URL in double quotes:
$ curl -X GET "http://localhost:8080/api/v1/appointments/findWithRange?start=2018-10-01&end=2018-10-15"
Get multiple RequestParam of one variable
this is how i did it:
if (CollectionUtils.isNotEmpty(lastName)) {
Criteria lastNameCriteria = new Criteria();
lastNameCriteria = lastNameCriteria.where("lastName").in(lastName.toArray());
criterias.add(lastNameCriteria);
}
How to pass multiple values in RequestParam
You need distinct List params, each one named after the query param.
given
i.e
request: /your-resource?param1=1,2,3¶m2=4,5¶m3=10
@Controller
@RequestMapping
public ResponseEntity<Object> foo(@RequestParam("params1") List<String> param1, @RequestParam("param2") List<String> params2, @RequestParam("param3") List<String> params3 )
Would result in 1,2,3 injected into param1. 4,5 into param2 and 10 into param3.
Multiple optional @RequestParam not fetching Data in spring boot
Finally, I solved the problem by JpaSpecificationExecutor which helps to create dynamic query. The code is given bellow.
UserSpecification.kt
import com.example.demo.entity.User
import org.springframework.data.jpa.domain.Specification
import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.CriteriaQuery
import javax.persistence.criteria.Root
import java.text.MessageFormat.format
object UserSpecification {
fun countryContains(country: String?): Specification<User>? {
return country?.let {
Specification { root: Root<User>, _: CriteriaQuery<*>, criteriaBuilder: CriteriaBuilder ->
criteriaBuilder.like(root.get("country"), format("%{0}%", country))
}
}
}
fun genderContains(gender: String?): Specification<User>? {
return gender?.let {
Specification { root: Root<User>, _: CriteriaQuery<*>, criteriaBuilder: CriteriaBuilder ->
criteriaBuilder.equal(root.get<String>("gender"), gender)
}
}
}
}
UserRepository.kt
@Repository
interface UserRepository: JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
}
UserServiceImpl.kt
@Service
class UserServiceImpl(
@Autowired private val userRepository: UserRepository
): UserService {
override fun getUsers(gender: String?, country: String?, pageable: Pageable): Page<User> {
return userRepository.findAll(
where(
countryContains(country)
).and(
genderContains(gender)
), pageable
)
}
}
multiple requestparam in springboot pageable
With Spring Boot you can just register PageableHandlerMethodArgumentResolver bean
@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
return new PageableHandlerMethodArgumentResolver();
}
In controller
public ResponseEntity<List<Test>> getAllTests(
@RequestParam (required = false) String search,
@PageableDefault(page = 0, size = 100) Pageable pageable
) {
...
}
And then adjust your query parameters to ...&page=0&size=2
Related Topics
How to Persist a Property of Type List<String> in Jpa
How to Properly Re-Run Spring Boot Application from Eclipse
How to Count the Rows in Hibernate Query Language
Passing Empty List as Parameter to JPA Query Throws Error
How to Use Limit in Spring Within SQL Query
I Get a Status 200 When Connecting to the Websocket, But It Is an Error
Maven 3.8.0 Compiler - Fatal Error Compiling: Release Version 11 Not Supported
How to Specify the Path for Getresourceasstream() Method in Java
Turning Sonar Off for Certain Code
Java: Getting a Substring from a String Starting After a Particular Character
Subscript and Superscript a String in Android
Making a Mocked Method Return an Argument That Was Passed to It
Read Huge Excel File(500K Rows) in Java
Webdriver/Pageobject/Findby: How to Specify Xpath With Dynamic Value