From b8651556c21c83448e46b270ed7013f490e53a5d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 29 Mar 2017 12:52:41 +0300 Subject: juick-www: inject json mapper --- .../com/juick/www/configuration/WwwAppConfiguration.java | 13 +++++++++++++ .../main/java/com/juick/www/controllers/FacebookLogin.java | 10 +++------- .../main/java/com/juick/www/controllers/TwitterAuth.java | 10 +++------- .../main/java/com/juick/www/controllers/VKontakteLogin.java | 10 +++------- 4 files changed, 22 insertions(+), 21 deletions(-) (limited to 'juick-www/src/main/java/com') diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java index 7ffefb1d..beb3f95f 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwAppConfiguration.java @@ -1,5 +1,8 @@ package com.juick.www.configuration; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.www.HelpService; @@ -47,4 +50,14 @@ public class WwwAppConfiguration { return new HelpService("help"); } + @Bean + public ObjectMapper jsonMapper() { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); + mapper.registerModule(new Jdk8Module()); + return mapper; + } + } diff --git a/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java b/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java index b1d275b6..178f6762 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java +++ b/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java @@ -17,7 +17,6 @@ */ package com.juick.www.controllers; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.scribejava.apis.FacebookApi; import com.github.scribejava.core.builder.ServiceBuilder; @@ -61,7 +60,8 @@ public class FacebookLogin { private final String FACEBOOK_APPID; private final String FACEBOOK_SECRET; private final String FACEBOOK_REDIRECT = "http://juick.com/_fblogin"; - private final ObjectMapper mapper; + @Inject + private ObjectMapper jsonMapper; private ServiceBuilder serviceBuilder; @Inject @@ -74,10 +74,6 @@ public class FacebookLogin { FACEBOOK_APPID = env.getProperty("facebook_appid"); FACEBOOK_SECRET = env.getProperty("facebook_secret"); serviceBuilder = new ServiceBuilder(); - mapper = new ObjectMapper(); - mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); } @RequestMapping(value = "/_fblogin", method = RequestMethod.GET) @@ -122,7 +118,7 @@ public class FacebookLogin { logger.error("FACEBOOK GRAPH ERROR"); throw new HttpBadRequestException(); } - User fb = mapper.readValue(graph, User.class); + User fb = jsonMapper.readValue(graph, User.class); long fbID = NumberUtils.toLong(fb.getId(), 0); if (fbID == 0 || StringUtils.isBlank(fb.getName()) || StringUtils.isBlank(fb.getLink())) { throw new HttpBadRequestException(); diff --git a/juick-www/src/main/java/com/juick/www/controllers/TwitterAuth.java b/juick-www/src/main/java/com/juick/www/controllers/TwitterAuth.java index 9b09539e..33f266b2 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/TwitterAuth.java +++ b/juick-www/src/main/java/com/juick/www/controllers/TwitterAuth.java @@ -1,6 +1,5 @@ package com.juick.www.controllers; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.scribejava.apis.TwitterApi; import com.github.scribejava.core.builder.ServiceBuilder; @@ -33,7 +32,8 @@ public class TwitterAuth { private String consumerKey, consumerSecret; - private final ObjectMapper mapper; + @Inject + private ObjectMapper jsonMapper; @Inject UserService userService; @@ -42,10 +42,6 @@ public class TwitterAuth { public TwitterAuth(Environment env) { this.consumerKey = env.getProperty("twitter_consumer_key"); this.consumerSecret = env.getProperty("twitter_consumer_secret"); - mapper = new ObjectMapper(); - mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); } @RequestMapping(value = "/_twitter", method = RequestMethod.GET) @@ -90,7 +86,7 @@ public class TwitterAuth { OAuth1AccessToken accessToken = oAuthService.getAccessToken(requestToken, verifier); OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, VERIFY_URL); oAuthService.signRequest(accessToken, oAuthRequest); - com.juick.www.twitter.User twitterUser = mapper.readValue(oAuthService.execute(oAuthRequest).getBody(), + com.juick.www.twitter.User twitterUser = jsonMapper.readValue(oAuthService.execute(oAuthRequest).getBody(), com.juick.www.twitter.User.class); if (userService.linkTwitterAccount(user, accessToken.getToken(), accessToken.getTokenSecret(), twitterUser.getScreenName())) { diff --git a/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java b/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java index e0a39220..efc86f7c 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java +++ b/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java @@ -17,7 +17,6 @@ */ package com.juick.www.controllers; -import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.scribejava.apis.VkontakteApi; import com.github.scribejava.core.builder.ServiceBuilder; @@ -71,13 +70,10 @@ public class VKontakteLogin { VK_APPID = env.getProperty("vk_appid"); VK_SECRET = env.getProperty("vk_secret"); serviceBuilder = new ServiceBuilder(); - mapper = new ObjectMapper(); - mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); } - private final ObjectMapper mapper; + @Inject + private ObjectMapper jsonMapper; @RequestMapping(value = "/_vklogin", method = RequestMethod.GET) protected String doGet(HttpServletRequest request, @@ -117,7 +113,7 @@ public class VKontakteLogin { vkService.signRequest(token, meRequest); String graph = vkService.execute(meRequest).getBody(); - com.juick.www.vk.User jsonUser = mapper.readValue(graph, UsersResponse.class).getUsers().get(0); + com.juick.www.vk.User jsonUser = jsonMapper.readValue(graph, UsersResponse.class).getUsers().get(0); String vkName = jsonUser.getFirstName() + " " + jsonUser.getLastName(); String vkLink = jsonUser.getScreenName(); -- cgit v1.2.3