Skip to content

Instantly share code, notes, and snippets.

@dogeared
Last active May 31, 2022 12:34
Show Gist options
  • Save dogeared/0db806ad56258711de0ffdfa5317ee42 to your computer and use it in GitHub Desktop.
Save dogeared/0db806ad56258711de0ffdfa5317ee42 to your computer and use it in GitHub Desktop.
application/x-www-form-urlencoded to POJO like a boss - HttpMessageConverter approach
@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
)
// notice, we can use @RequestBody with this approach
public @ResponseBody SlackSlashCommand slack2(@RequestBody SlackSlashCommand slackSlashCommand) {
log.info("slackSlashCommand: {}", slackSlashCommand);
return slackSlashCommand;
}
}
public class SlackSlashCommand {
private String token;
private String command;
private String text;
// use of @JsonProperty allows this to be returned from the controller with the proper jackson mappings
@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();
}
}
public class SlackSlashCommandConverter extends AbstractHttpMessageConverter<SlackSlashCommand> {
// no need to reinvent the wheel for parsing the query string
private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
private static final ObjectMapper mapper = new ObjectMapper();
@Override
protected boolean supports(Class<?> clazz) {
return (SlackSlashCommand.class == clazz);
}
@Override
protected SlackSlashCommand readInternal(Class<? extends SlackSlashCommand> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
Map<String, String> vals = formHttpMessageConverter.read(null, inputMessage).toSingleValueMap();
return mapper.convertValue(vals, SlackSlashCommand.class);
}
@Override
protected void writeInternal(SlackSlashCommand slackSlashCommand, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
}
}
@Configuration
public class SlackSlashCommandConverterConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
SlackSlashCommandConverter slackSlashCommandConverter = new SlackSlashCommandConverter();
MediaType mediaType = new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"));
slackSlashCommandConverter.setSupportedMediaTypes(Arrays.asList(mediaType));
converters.add(slackSlashCommandConverter);
super.configureMessageConverters(converters);
}
}
@tartak2509
Copy link

Good example, thanks :)

@dalaiRama06
Copy link

Really good example, Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment