Added boot app, filters, controller and model (#3848)
This commit is contained in:
parent
87e4c25f45
commit
63a220d39a
|
@ -0,0 +1,17 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
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.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);
|
||||
|
||||
@RequestMapping("")
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
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