您可以使用setter来实现。以您的示例为例:
@SpringBootApplication
public class So44390404Application {
public static void main(String[] args) {
SpringApplication.run(So44390404Application.class, args);
}
@RestController
public static class MyServlet {
@GetMapping
public String test(MyReq req) {
return req.toString();
}
}
public static class MyReq {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDifferent_Name(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "{" + name + age + '}';
}
}
}
调用者可能会使用:
$so44390404 curl -XGET 'http://localhost:8000?name=adam&age=42'
{adam42}%
$so44390404 curl -XGET 'http://localhost:8000?Different_Name=John&age=23'
{John23}%
更新
如果你正在处理带连字符参数的问题,事情可能会变得有些棘手。
基本上你可以:
- 创建一个过滤器来
规范化
带连字符的参数名称,以便Spring能够成功绑定它们。
- 在控制器中将所有请求参数作为原始映射接收,
规范化
键,然后自己使用所有类型转换功能填充对象。
使用过滤器的选项可能如下所示:
@Component
public static class CustomRequestParametersFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
filterChain.doFilter(new RequestParameterNormalizerWrapper(request), response);
}
public static class RequestParameterNormalizerWrapper extends HttpServletRequestWrapper {
public static final String HYPHEN = "-";
private final Map<String, String[]> parameterMap = new HashMap<>();
public RequestParameterNormalizerWrapper(HttpServletRequest request) {
super(request);
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
if (entry.getKey().contains(HYPHEN)) {
parameterMap.put(normalize(entry.getKey()), entry.getValue());
}
else {
parameterMap.put(entry.getKey(), entry.getValue());
}
}
}
private String normalize(final String key) {
if (key.contains(HYPHEN)) {
return WordUtils.capitalizeFully(key, HYPHEN.charAt(0)).replaceAll(HYPHEN, "");
}
return key;
}
@Override
public Map<String, String[]> getParameterMap() {
return Collections.unmodifiableMap(this.parameterMap);
}
@Override
public Enumeration<String> getParameterNames() {
return Collections.enumeration(this.parameterMap.keySet());
}
@Override
public String getParameter(String name) {
return super.getParameter(normalize(name));
}
@Override
public String[] getParameterValues(String name) {
return parameterMap.get(normalize(name));
}
}
}
通过上一个例子,应该可以直接使用。
第二个选项可能是:
@RestController
public static class MyServlet {
@GetMapping
public String test(@RequestParam Map<String, String> pvs) {
final MyReq req = new MyReq();
final BeanWrapper beanWrapper = new HyphenAwareBeanWrapper(req);
beanWrapper.setPropertyValues(pvs);
return req.toString();
}
}
并且包装器:
public static class HyphenAwareBeanWrapper extends BeanWrapperImpl {
public static final String HYPHEN = "-";
public HyphenAwareBeanWrapper(Object object) {
super(object);
}
@Override
public void setPropertyValues(Map<?, ?> map) throws BeansException {
final ArrayList<PropertyValue> propertyValueList = new ArrayList<>(map.size());
for (Map.Entry<?, ?> entry : map.entrySet()) {
final String key = entry.getKey().toString().contains(HYPHEN)
? WordUtils.capitalizeFully(entry.getKey().toString(), HYPHEN.charAt(0)).replaceAll(HYPHEN, "")
: entry.getKey().toString();
propertyValueList.add(new PropertyValue(key, entry.getValue()));
}
super.setPropertyValues(new MutablePropertyValues(propertyValueList));
}
}
测试:
$ curl -XGET 'http://localhost:8000?name=John&age=42'
{John42}%
$ curl -XGET 'http://localhost:8000?different-name=John&age=42'
{John42}%