Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spring Boot: convert inbound parameters from snake_case to camelCase
@Configuration
public class SnakeCaseApplicationConfiguration {
@Bean
public OncePerRequestFilter snakeCaseConverterFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
final Map<String, String[]> parameters = new ConcurrentHashMap<>();
for (String param : request.getParameterMap().keySet()) {
String camelCaseParam = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, param);
parameters.put(camelCaseParam, request.getParameterValues(param));
parameters.put(param, request.getParameterValues(param));
}
filterChain.doFilter(new HttpServletRequestWrapper(request) {
@Override
public String getParameter(String name) {
return parameters.containsKey(name) ? parameters.get(name)[0] : null;
}
@Override
public Enumeration<String> getParameterNames() {
return Collections.enumeration(parameters.keySet());
}
@Override
public String[] getParameterValues(String name) {
return parameters.get(name);
}
@Override
public Map<String, String[]> getParameterMap() {
return parameters;
}
}, response);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.