Reputation: 220
I have a RESTful service. This takes in two parameters a start-date and end-date.
If I use @RequestParam annotation I get what I need. But if I use @QueryParam I noticed that its always showing as null even when passed.
Here is my resource:
@RequestMapping(value = "/usage-query", method = RequestMethod.GET)
@ApiOperation(value = "Available Sessions - JSON Body", notes = "GET method for unique users")
public List<DTO> getUsageByDate(@QueryParam("start-date") final String startDate,
@QueryParam("end-date") String endDate)
throws BadParameterException {
return aaaService.findUsageByDate2(startDate, endDate);
}
Then here is my service:
List<DTO> findUsageByDate(String startDate, String endDate) throws BadParameterException;
Then here is my service implementation:
public List<DTO> findUsageByDate(String startDate, String endDate) throws BadParameterException {
return aaaDao.getUsageByDate(startDate, endDate);
}
Here is my DAO:
List<DTO> getUsageByDate(String startDate, String endDate) throws BadParameterException;
Here is my DAO Implementation:
@Override
public List<DTO> getUsageByDate(String startDate, String endDate) throws BadParameterException {
StringBuilder sql = new StringBuilder(
"select * from usage where process_time >= :start_date");
if(endDate!= null)
{
sql.append(" and process_time < :end_date");
}
sql.append(" limit 10");
System.out.println(sql.toString());
SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("start_date", startDate)
.addValue("end_date", endDate);
try {
return jdbcTemplate.query(sql.toString(), namedParameters,
new BeanPropertyRowMapper<DTO>(AAAUsageDTO.class));
} catch (EmptyResultDataAccessException e) {
throw new BadParameterException();
}
}
Any help would be greatly appreciated. Probably something obvious
Upvotes: 0
Views: 3909
Reputation: 208994
If I use
@RequestParam
annotation I get what I need. But if I use@QueryParam
I noticed that its always showing as null even when passed.
Because you are using Spring MVC, which has no connection whatsoever to JAX-RS, which @QueryParam
is for. Spring uses @RequestParam
. If you're going to use Spring, I suggest you get rid of your JAX-RS dependency so you don't get confused about what you can and can't use.
Upvotes: 5
Reputation: 41
Replace @QueryParam("end-date")
with @QueryParam("endDate")
. This will work. But, I would suggest you to use @RequestParam
from org.springframework.web.bind.annotation
, the reason for which is well explained in the answer by Paul Samsotha.
Upvotes: 0