2016-04-03 170 views
0

我有一个RESTful服务。这需要两个参数:开始日期和结束日期。@QueryParam始终显示为空

如果我使用@RequestParam注释,我得到我需要的。但是如果我使用@QueryParam,我注意到它即使在传递时也总是显示为空。

这里是我的资源:

@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); 

} 

那么这里就是我的服务:

List<DTO> findUsageByDate(String startDate, String endDate) throws BadParameterException; 

那么这里就是我的服务实现:

public List<DTO> findUsageByDate(String startDate, String endDate) throws BadParameterException { 
    return aaaDao.getUsageByDate(startDate, endDate); 

} 

这里是我的DAO:

List<DTO> getUsageByDate(String startDate, String endDate) throws BadParameterException; 

这里是我的DAO实现:

@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(); 
    } 
} 

任何帮助将不胜感激。可能有些东西明显

+0

以及您要调用requestparam和query param的服务端点是什么? – Sanj

+0

对不起,我把代码放在了需要的位置:-)这里是:GET/v1/usage/usage- query?start-date = 2016-01-01; end-date = 2016-03- 01 HTTP/1.1 – Xathras

+0

在开始日期和结束日期之间有一个&符号? – Sanj

回答

2

如果我使用@RequestParam注释我得到我需要的东西。但是,如果我使用@QueryParam,我注意到即使通过,其始终显示为空。

因为你正在使用Spring MVC的,其中有任何没有连接到JAX-RS,这@QueryParam是。春季使用@RequestParam。如果您打算使用Spring,我建议您摆脱JAX-RS依赖关系,因此您不会对可以使用和不能使用的内容感到困惑。

+0

非常感谢你是的,这使得很多感觉 – Xathras