Created
October 15, 2015 15:32
-
-
Save azhawkes/3db84b194b3e47423df2 to your computer and use it in GitHub Desktop.
Spring Boot: convert inbound parameters from snake_case to camelCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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