@QueryParam 总是显示 null

Xat*_*ras 0 java rest spring

我有一个 RESTful 服务。这需要两个参数:开始日期和结束日期。

如果我使用 @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);

}
Run Code Online (Sandbox Code Playgroud)

然后这是我的服务:

    List<DTO> findUsageByDate(String startDate, String endDate) throws BadParameterException;
Run Code Online (Sandbox Code Playgroud)

然后这是我的服务实现:

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

}
Run Code Online (Sandbox Code Playgroud)

这是我的 DAO:

    List<DTO> getUsageByDate(String startDate, String endDate) throws BadParameterException;
Run Code Online (Sandbox Code Playgroud)

这是我的 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。可能是显而易见的事情

Pau*_*tha 5

如果我使用@RequestParam注释,我就会得到我需要的东西。但如果我使用@QueryParam我注意到它总是显示为空,即使通过了。

因为您使用的是 Spring MVC,它与 JAX-RS没有任何联系@QueryParam。弹簧使用@RequestParam. 如果您打算使用 Spring,我建议您摆脱 JAX-RS 依赖关系,这样您就不会混淆可以使用什么和不能使用什么。