java - 在一个类中收集多个查询参数

标签 java spring spring-mvc spring-boot

我正在使用 spring boot 1.3.6 实现 Web 服务。在我的 Controller 中,我有一个类似的方法:

@RequestMapping(value = "/employees/{id}", method = RequestMethod.PUT)    
createEmployee(@PathVariable String id, 
               @QueryParam(required = false, value = "department") Set<String> departments)

我想在类中收集请求参数,例如:

class EmployeeParams {
    public String id;
    public Set<String> departments;
}

我尝试使用:

@RequestMapping(value = "/employees/{id}", method = RequestMethod.PUT)    
createEmployee(EmployeeParams param) { ... } 

但它不起作用。我在上面的类(class)中获得了 id,但不是部门。 Spring请求中请求参数的正确收集方式是什么?

最佳答案

您应该添加一个实现 Spring org.springframework.core.convert.converter.Converter 的自定义转换器,并向 Spring 注册它。

参见 Spring documentation .
This stack overflow issue还讨论了有关添加自定义转换器或格式化程序的一些细节。

关于java - 在一个类中收集多个查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38528187/

相关文章:

java - 以编程方式确定当前平台上可用的 Java 语言功能的任何方法?

Spring @Autowire on Properties vs Constructor

java - 无法停止 Spring 批处理步骤

java - 如何使用 POI 库获取 Excel 文件中的行数?

java - 将带有 JSON 数据的授权 curl -u post 请求转换为等效的 RestTemplate

java - JDK 1.4和JDK1.6有什么区别

java - 为什么我们需要在 <persistence-unit> 元素中指定类?

java - Spring 对缺少参数的 MVC 方法的建议

spring - @Autowired 在静态类中

java - 使用 Spring MVC 和 Spring Security 在 Multi-Tenancy Web 应用程序中使用表单登录