JAVA-86: Remove spring-boot-add-filter
This commit is contained in:
parent
802d1d3b90
commit
a5ee0d6b6f
|
@ -22,7 +22,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [An Introduction to Kong](https://www.baeldung.com/kong)
|
||||
- [Spring Boot: Configuring a Main Class](https://www.baeldung.com/spring-boot-main-class)
|
||||
- [A Quick Intro to the SpringBootServletInitializer](https://www.baeldung.com/spring-boot-servlet-initializer)
|
||||
- [How to Define a Spring Boot Filter?](https://www.baeldung.com/spring-boot-add-filter)
|
||||
- [Spring Boot Exit Codes](https://www.baeldung.com/spring-boot-exit-codes)
|
||||
- [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon)
|
||||
- [Spring Shutdown Callbacks](https://www.baeldung.com/spring-shutdown-callbacks)
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
package com.baeldung.bootcustomfilters;
|
||||
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.bootcustomfilters.filters.RequestResponseLoggingFilter;
|
||||
|
||||
@Configuration
|
||||
public class FilterConfig {
|
||||
|
||||
// uncomment this and comment the @Component in the filter class definition to register only for a url pattern
|
||||
// @Bean
|
||||
public FilterRegistrationBean<RequestResponseLoggingFilter> loggingFilter() {
|
||||
FilterRegistrationBean<RequestResponseLoggingFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
|
||||
registrationBean.setFilter(new RequestResponseLoggingFilter());
|
||||
|
||||
registrationBean.addUrlPatterns("/users/*");
|
||||
|
||||
return registrationBean;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package com.baeldung.bootcustomfilters;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Boot application
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class SpringBootFiltersApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootFiltersApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.baeldung.bootcustomfilters.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.bootcustomfilters.model.User;
|
||||
|
||||
/**
|
||||
* Rest controller for User
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
@GetMapping("")
|
||||
public List<User> getAllUsers() {
|
||||
LOG.info("Fetching all the users");
|
||||
return Arrays.asList(
|
||||
new User(UUID.randomUUID().toString(), "User1", "user1@test.com"),
|
||||
new User(UUID.randomUUID().toString(), "User1", "user1@test.com"),
|
||||
new User(UUID.randomUUID().toString(), "User1", "user1@test.com"));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung.bootcustomfilters.filters;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* A servlet filter to log request and response
|
||||
* The logging implementation is pretty native and for demonstration only
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Order(2)
|
||||
public class RequestResponseLoggingFilter implements Filter {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(RequestResponseLoggingFilter.class);
|
||||
|
||||
@Override
|
||||
public void init(final FilterConfig filterConfig) throws ServletException {
|
||||
LOG.info("Initializing filter :{}", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
HttpServletRequest req = (HttpServletRequest) request;
|
||||
HttpServletResponse res = (HttpServletResponse) response;
|
||||
LOG.info("Logging Request {} : {}", req.getMethod(), req.getRequestURI());
|
||||
chain.doFilter(request, response);
|
||||
LOG.info("Logging Response :{}", res.getContentType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
LOG.warn("Destructing filter :{}", this);
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package com.baeldung.bootcustomfilters.filters;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* A filter to create transaction before and commit it once request completes
|
||||
* The current implemenatation is just for demo
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Order(1)
|
||||
public class TransactionFilter implements Filter {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(TransactionFilter.class);
|
||||
|
||||
@Override
|
||||
public void init(final FilterConfig filterConfig) throws ServletException {
|
||||
LOG.info("Initializing filter :{}", this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletRequest req = (HttpServletRequest) request;
|
||||
LOG.info("Starting Transaction for req :{}", req.getRequestURI());
|
||||
chain.doFilter(request, response);
|
||||
LOG.info("Committing Transaction for req :{}", req.getRequestURI());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
LOG.warn("Destructing filter :{}", this);
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package com.baeldung.bootcustomfilters.model;
|
||||
|
||||
/**
|
||||
* User model
|
||||
* @author hemant
|
||||
*
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public User(String id, String name, String email) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue