changing the wiring to autowire

This commit is contained in:
eugenp 2015-02-27 21:17:24 +02:00
parent c89cbd6b2b
commit bace2a4416
3 changed files with 13 additions and 26 deletions

View File

@ -2,10 +2,9 @@ package org.baeldung.config;
import java.util.Arrays;
import org.baeldung.web.RedditController;
import org.baeldung.web.RestExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@ -28,6 +27,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "org.baeldung.web" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
@ -48,18 +48,6 @@ public class WebConfig extends WebMvcConfigurerAdapter {
configurer.enable();
}
@Bean
public RedditController redditController(OAuth2RestTemplate redditRestTemplate) {
RedditController controller = new RedditController();
controller.setRedditRestTemplate(redditRestTemplate);
return controller;
}
@Bean
public RestExceptionHandler restExceptionHandler() {
return new RestExceptionHandler();
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

View File

@ -8,6 +8,7 @@ import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@ -22,12 +23,16 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
public class RedditController {
private OAuth2RestTemplate redditRestTemplate;
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
@Autowired
private OAuth2RestTemplate redditRestTemplate;
// API
@RequestMapping("/info")
public String getInfo(Model model) {
public final String getInfo(Model model) {
JsonNode node = redditRestTemplate.getForObject("https://oauth.reddit.com/api/v1/me", JsonNode.class);
String name = node.get("name").asText();
model.addAttribute("info", name);
@ -35,7 +40,7 @@ public class RedditController {
}
@RequestMapping("/submit")
public String submit(Model model, @RequestParam Map<String, String> formParams) {
public final String submit(Model model, @RequestParam Map<String, String> formParams) {
MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
param.add("api_type", "json");
param.add("kind", "link");
@ -56,7 +61,7 @@ public class RedditController {
}
@RequestMapping("/post")
public String showSubmissionForm(Model model) {
public final String showSubmissionForm(Model model) {
String needsCaptchaResult = needsCaptcha();
if (needsCaptchaResult.equalsIgnoreCase("true")) {
String iden = getNewCaptcha();
@ -67,7 +72,7 @@ public class RedditController {
// === private
public List<String> getSubreddit() throws JsonProcessingException, IOException {
List<String> getSubreddit() throws JsonProcessingException, IOException {
String result = redditRestTemplate.getForObject("https://oauth.reddit.com/subreddits/popular?limit=50", String.class);
JsonNode node = new ObjectMapper().readTree(result);
node = node.get("data").get("children");
@ -108,8 +113,4 @@ public class RedditController {
}
}
public void setRedditRestTemplate(OAuth2RestTemplate redditRestTemplate) {
this.redditRestTemplate = redditRestTemplate;
}
}

View File

@ -14,9 +14,6 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3861125729653781371L;
public RestExceptionHandler() {
@ -39,4 +36,5 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler impleme
String response = "Error Occurred : " + ex.getMessage();
return handleExceptionInternal(ex, response, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}
}