Last active
May 22, 2017 21:48
-
-
Save dogeared/7e60a2caebd00c959f0d7e24ef79b54e to your computer and use it in GitHub Desktop.
application/x-www-form-urlencoded to POJO like a boss - Abstract super class 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
// workaround for customize x-www-form-urlencoded | |
public abstract class AbstractFormSlackSlashCommand { | |
// eww - breaks java code convetion, but doesn't require any additional configuration | |
public void setTeam_id(String teamId) { | |
setTeamId(teamId); | |
} | |
public void setTeam_domain(String teamDomain) { | |
setTeamDomain(teamDomain); | |
} | |
public void setChannel_id(String channelId) { | |
setChannelId(channelId); | |
} | |
public void setChannel_name(String channelName) { | |
setChannelName(channelName); | |
} | |
public void setUser_id(String userId) { | |
setUserId(userId); | |
} | |
public void setUser_name(String userName) { | |
setUserName(userName); | |
} | |
public void setResponse_url(String responseUrl) { | |
setResponseUrl(responseUrl); | |
} | |
abstract void setTeamId(String teamId); | |
abstract void setTeamDomain(String teamDomain); | |
abstract void setChannelId(String channelId); | |
abstract void setChannelName(String channelName); | |
abstract void setUserId(String userId); | |
abstract void setUserName(String userName); | |
abstract void setResponseUrl(String responseUrl); | |
} |
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 | |
) | |
// notice, no @RequestBody with this approach | |
public @ResponseBody SlackSlashCommand slack2(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 extends AbstractFormSlackSlashCommand { | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment