Created
May 24, 2017 13:36
-
-
Save dogeared/3ebb46b9d948c023e702ccb9ecfdf35e to your computer and use it in GitHub Desktop.
application/x-www-form-urlencoded to POJO like a boss - HandlerMethodArgumentResolver approach
This file contains hidden or 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
@RestController | |
@RequestMapping("/api/v1") | |
public class SlackController { | |
private static final Logger log = LoggerFactory.getLogger(SlackController.class); | |
@RequestMapping( | |
value = "/slack", method = RequestMethod.POST, | |
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE | |
) | |
public @ResponseBody SlackSlashCommand slack(SlackSlashCommand slackSlashCommand) { | |
log.info("slackSlashCommand: {}", slackSlashCommand); | |
return slackSlashCommand; | |
} | |
} |
This file contains hidden or 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
public class SlackSlashCommand { | |
private String token; | |
private String command; | |
private String text; | |
@JsonProperty("team_id") | |
private String teamId; | |
@JsonProperty("team_domain") | |
private String teamDomain; | |
@JsonProperty("channel_id") | |
private String channelId; | |
@JsonProperty("channel_name") | |
private String channelName; | |
@JsonProperty("user_id") | |
private String userId; | |
@JsonProperty("user_name") | |
private String userName; | |
@JsonProperty("response_url") | |
private String responseUrl; | |
public String getToken() { | |
return token; | |
} | |
public void setToken(String token) { | |
this.token = token; | |
} | |
public String getTeamId() { | |
return teamId; | |
} | |
public void setTeamId(String team_id) { | |
this.teamId = team_id; | |
} | |
public String getTeamDomain() { | |
return teamDomain; | |
} | |
public void setTeamDomain(String teamDomain) { | |
this.teamDomain = teamDomain; | |
} | |
public String getChannelId() { | |
return channelId; | |
} | |
public void setChannelId(String channelId) { | |
this.channelId = channelId; | |
} | |
public String getChannelName() { | |
return channelName; | |
} | |
public void setChannelName(String channelName) { | |
this.channelName = channelName; | |
} | |
public String getUserId() { | |
return userId; | |
} | |
public void setUserId(String userId) { | |
this.userId = userId; | |
} | |
public String getUserName() { | |
return userName; | |
} | |
public void setUserName(String userName) { | |
this.userName = userName; | |
} | |
public String getCommand() { | |
return command; | |
} | |
public void setCommand(String command) { | |
this.command = command; | |
} | |
public String getText() { | |
return text; | |
} | |
public void setText(String text) { | |
this.text = text; | |
} | |
public String getResponseUrl() { | |
return responseUrl; | |
} | |
public void setResponseUrl(String responseUrl) { | |
this.responseUrl = responseUrl; | |
} | |
public String toString() { | |
return | |
getToken() + "|" + getCommand() + "|" + getText() + "|" + getTeamId() + "|" + getTeamDomain() + "|" + | |
getChannelId() + "|" + getChannelName() + "|" + getUserId() + "|" + getUserName() + "|" + | |
getResponseUrl(); | |
} | |
} |
This file contains hidden or 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
public class SlackSlashCommandMethodArgumentResolver implements HandlerMethodArgumentResolver { | |
@Override | |
public boolean supportsParameter(MethodParameter methodParameter) { | |
return methodParameter.getParameterType().equals(SlackSlashCommand.class); | |
} | |
@Override | |
public Object resolveArgument( | |
MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, | |
NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory | |
) throws Exception { | |
SlackSlashCommand ret = new SlackSlashCommand(); | |
ret.setChannelId(nativeWebRequest.getParameter("channel_id")); | |
ret.setChannelName(nativeWebRequest.getParameter("channel_name")); | |
ret.setCommand(nativeWebRequest.getParameter("command")); | |
ret.setResponseUrl(nativeWebRequest.getParameter("response_url")); | |
ret.setTeamDomain(nativeWebRequest.getParameter("team_domain")); | |
ret.setTeamId(nativeWebRequest.getParameter("team_id")); | |
ret.setText(nativeWebRequest.getParameter("text")); | |
ret.setToken(nativeWebRequest.getParameter("token")); | |
ret.setUserId(nativeWebRequest.getParameter("user_id")); | |
ret.setUserName(nativeWebRequest.getParameter("user_name")); | |
return ret; | |
} | |
private boolean isNotSet(String value) { | |
return value == null; | |
} | |
} |
This file contains hidden or 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 SlackSlashCommandMethodArgumentResolverConfig extends WebMvcConfigurerAdapter { | |
@Override | |
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { | |
argumentResolvers.add(new SlackSlashCommandMethodArgumentResolver()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment