Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-17473

This commit is contained in:
amit2103
2019-10-06 11:33:23 +05:30
432 changed files with 2349 additions and 2256 deletions

View File

@@ -0,0 +1,17 @@
package com.baeldung.exclude_urls_filter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan(basePackages = "com.baeldung.exclude_urls_filter")
@Configuration
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.exclude_urls_filter.controller;
import com.baeldung.exclude_urls_filter.service.FAQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FAQController {
private final FAQService faqService;
@Autowired
public FAQController(FAQService faqService) {
this.faqService = faqService;
}
@RequestMapping(value = "/faq/helpline", method = RequestMethod.GET)
public ResponseEntity<String> getHelpLineNumber() {
String helplineNumber = faqService.getHelpLineNumber();
if (helplineNumber != null) {
return new ResponseEntity<String>(helplineNumber, HttpStatus.OK);
} else {
return new ResponseEntity<String>("Unavailable", HttpStatus.NOT_FOUND);
}
}
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.exclude_urls_filter.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Ping {
@RequestMapping(value = "/health", method = RequestMethod.GET)
public ResponseEntity<String> pingGet() {
return new ResponseEntity<String>("pong", HttpStatus.OK);
}
@RequestMapping(value = "/health", method = RequestMethod.POST)
public ResponseEntity<String> pingPost() {
return new ResponseEntity<String>("pong", HttpStatus.OK);
}
}

View File

@@ -0,0 +1,26 @@
package com.baeldung.exclude_urls_filter.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterRegistrationConfig {
@Bean
public FilterRegistrationBean<LogFilter> logFilter() {
FilterRegistrationBean<LogFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new LogFilter());
registrationBean.addUrlPatterns("/health", "/faq/*");
return registrationBean;
}
@Bean
public FilterRegistrationBean<HeaderValidatorFilter> headerValidatorFilter() {
FilterRegistrationBean<HeaderValidatorFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new HeaderValidatorFilter());
registrationBean.addUrlPatterns("*");
return registrationBean;
}
}

View File

@@ -0,0 +1,30 @@
package com.baeldung.exclude_urls_filter.filter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Order(1)
public class HeaderValidatorFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String path = request.getRequestURI();
if ("/health".equals(path)) {
filterChain.doFilter(request, response);
return;
}
String countryCode = request.getHeader("X-Country-Code");
if (!"US".equals(countryCode)) {
response.sendError(HttpStatus.BAD_REQUEST.value(), "Invalid Locale");
return;
}
filterChain.doFilter(request, response);
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.exclude_urls_filter.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Order(1)
public class LogFilter extends OncePerRequestFilter {
private final Logger logger = LoggerFactory.getLogger(LogFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
String path = request.getRequestURI();
String contentType = request.getContentType();
logger.info("Request URL path : {}, Request content type: {}", path, contentType);
filterChain.doFilter(request, response);
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.exclude_urls_filter.service;
public interface FAQService {
String getHelpLineNumber();
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.exclude_urls_filter.service;
import org.springframework.stereotype.Service;
@Service
public class FAQServiceImpl implements FAQService {
private static final String HELPLINE_NUMBER = "+1 888-777-66";
@Override
public String getHelpLineNumber() {
return HELPLINE_NUMBER;
}
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.baeldung.spring.config.converter.StringToEnumConverter;
@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
public MvcConfig() {
super();
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToEnumConverter());
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.spring.config.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import com.baeldung.spring.model.Modes;
@Component
public class StringToEnumConverter implements Converter<String, Modes> {
@Override
public Modes convert(String source) {
// Remove the try-catch block if we want to handle the exception globally in GlobalControllerExceptionHandler
try {
return Modes.valueOf(source.toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.spring.enums;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.spring.model.Modes;
@RestController
@RequestMapping("/enums")
public class EnumController {
@GetMapping("/mode2str")
public String getStringToMode(@RequestParam("mode") Modes mode) {
return "good";
}
@GetMapping("/findbymode/{mode}")
public String findByEnum(@PathVariable Modes mode) {
return "good";
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.spring.exceptions;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalControllerExceptionHandler {
@ExceptionHandler(ConversionFailedException.class)
public ResponseEntity<String> handleConflict(RuntimeException ex) {
// Remove the try-catch block in the StringToEnumConverter if we want to handle the exception here
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.spring.model;
public enum Modes {
ALPHA, BETA;
}