BAEL-2906 Functional Controllers in Spring MVC (#6972)
* BAEL-2906 Functional Controllers in Spring MVC * BAEL-2906 Functional Controllers in Spring MVC * BAEL-2906 Functional Controllers in Spring MVC * BAEL-2906 Removed unnecessary files * BAEL-2906 Removed project * BAEL-2906 Removed comments
This commit is contained in:
parent
8f5bb3c6a9
commit
d9a20d1764
25
spring-boot-mvc-2/.gitignore
vendored
Normal file
25
spring-boot-mvc-2/.gitignore
vendored
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/build/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
3
spring-boot-mvc-2/README.md
Normal file
3
spring-boot-mvc-2/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
|
||||||
|
- [Functional Controllers in Spring MVC]()
|
68
spring-boot-mvc-2/pom.xml
Normal file
68
spring-boot-mvc-2/pom.xml
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spring-boot-mvc-2</artifactId>
|
||||||
|
<name>spring-boot-mvc-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<description>Module For Spring Boot MVC Web Fn</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.2.0.BUILD-SNAPSHOT</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.baeldung.springbootmvc.SpringBootMvcFnApplication</mainClass>
|
||||||
|
<layout>JAR</layout>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
<pluginRepositories>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</pluginRepository>
|
||||||
|
<pluginRepository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
</pluginRepository>
|
||||||
|
</pluginRepositories>
|
||||||
|
</project>
|
@ -0,0 +1,74 @@
|
|||||||
|
package com.baeldung.springbootmvc;
|
||||||
|
|
||||||
|
import static org.springframework.web.servlet.function.RouterFunctions.route;
|
||||||
|
import static org.springframework.web.servlet.function.ServerResponse.notFound;
|
||||||
|
import static org.springframework.web.servlet.function.ServerResponse.status;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.servlet.function.RequestPredicates;
|
||||||
|
import org.springframework.web.servlet.function.RouterFunction;
|
||||||
|
import org.springframework.web.servlet.function.ServerResponse;
|
||||||
|
|
||||||
|
import com.baeldung.springbootmvc.ctrl.ProductController;
|
||||||
|
import com.baeldung.springbootmvc.svc.ProductService;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringBootMvcFnApplication {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(SpringBootMvcFnApplication.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootMvcFnApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
RouterFunction<ServerResponse> productListing(ProductController pc, ProductService ps) {
|
||||||
|
return pc.productListing(ps);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
RouterFunction<ServerResponse> allApplicationRoutes(ProductController pc, ProductService ps) {
|
||||||
|
return route().add(pc.remainingProductRoutes(ps))
|
||||||
|
.before(req -> {
|
||||||
|
LOG.info("Found a route which matches " + req.uri()
|
||||||
|
.getPath());
|
||||||
|
return req;
|
||||||
|
})
|
||||||
|
.after((req, res) -> {
|
||||||
|
if (res.statusCode() == HttpStatus.OK) {
|
||||||
|
LOG.info("Finished processing request " + req.uri()
|
||||||
|
.getPath());
|
||||||
|
} else {
|
||||||
|
LOG.info("There was an error while processing request" + req.uri());
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
})
|
||||||
|
.onError(Throwable.class, (e, res) -> {
|
||||||
|
LOG.error("Fatal exception has occurred", e);
|
||||||
|
return status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
.and(route(RequestPredicates.all(), req -> notFound().build()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Error {
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public Error(String message) {
|
||||||
|
this.errorMessage = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.springbootmvc.ctrl;
|
||||||
|
|
||||||
|
import static org.springframework.web.servlet.function.RouterFunctions.route;
|
||||||
|
import static org.springframework.web.servlet.function.ServerResponse.ok;
|
||||||
|
import static org.springframework.web.servlet.function.ServerResponse.status;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.function.EntityResponse;
|
||||||
|
import org.springframework.web.servlet.function.RequestPredicates;
|
||||||
|
import org.springframework.web.servlet.function.RouterFunction;
|
||||||
|
import org.springframework.web.servlet.function.ServerRequest;
|
||||||
|
import org.springframework.web.servlet.function.ServerResponse;
|
||||||
|
|
||||||
|
import com.baeldung.springbootmvc.SpringBootMvcFnApplication.Error;
|
||||||
|
import com.baeldung.springbootmvc.model.Product;
|
||||||
|
import com.baeldung.springbootmvc.svc.ProductService;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ProductController {
|
||||||
|
|
||||||
|
public RouterFunction<ServerResponse> productListing(ProductService ps) {
|
||||||
|
return route().GET("/product", req -> ok().body(ps.findAll()))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RouterFunction<ServerResponse> productSearch(ProductService ps) {
|
||||||
|
return route().nest(RequestPredicates.path("/product"), builder -> {
|
||||||
|
builder.GET("/name/{name}", req -> ok().body(ps.findByName(req.pathVariable("name"))))
|
||||||
|
.GET("/id/{id}", req -> ok().body(ps.findById(Integer.parseInt(req.pathVariable("id")))));
|
||||||
|
})
|
||||||
|
.onError(ProductService.ItemNotFoundException.class, (e, req) -> EntityResponse.fromObject(new Error(e.getMessage()))
|
||||||
|
.status(HttpStatus.NOT_FOUND)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RouterFunction<ServerResponse> adminFunctions(ProductService ps) {
|
||||||
|
return route().POST("/product", req -> ok().body(ps.save(req.body(Product.class))))
|
||||||
|
.filter((req, next) -> authenticate(req) ? next.handle(req) : status(HttpStatus.UNAUTHORIZED).build())
|
||||||
|
.onError(IllegalArgumentException.class, (e, req) -> EntityResponse.fromObject(new Error(e.getMessage()))
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RouterFunction<ServerResponse> remainingProductRoutes(ProductService ps) {
|
||||||
|
return route().add(productSearch(ps))
|
||||||
|
.add(adminFunctions(ps))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean authenticate(ServerRequest req) {
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.baeldung.springbootmvc.model;
|
||||||
|
|
||||||
|
public class Product {
|
||||||
|
private String name;
|
||||||
|
private double price;
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public Product(String name, double price, int id) {
|
||||||
|
super();
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + id;
|
||||||
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
|
long temp;
|
||||||
|
temp = Double.doubleToLongBits(price);
|
||||||
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Product other = (Product) obj;
|
||||||
|
if (id != other.id)
|
||||||
|
return false;
|
||||||
|
if (name == null) {
|
||||||
|
if (other.name != null)
|
||||||
|
return false;
|
||||||
|
} else if (!name.equals(other.name))
|
||||||
|
return false;
|
||||||
|
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.baeldung.springbootmvc.svc;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import com.baeldung.springbootmvc.model.Product;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ProductService {
|
||||||
|
|
||||||
|
private final Set<Product> products = new HashSet<>();
|
||||||
|
|
||||||
|
{
|
||||||
|
products.add(new Product("Book", 23.90, 1));
|
||||||
|
products.add(new Product("Pen", 44.34, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product findById(int id) {
|
||||||
|
return products.stream()
|
||||||
|
.filter(obj -> obj.getId() == id)
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow(() -> new ItemNotFoundException("Product not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product findByName(String name) {
|
||||||
|
return products.stream()
|
||||||
|
.filter(obj -> obj.getName()
|
||||||
|
.equalsIgnoreCase(name))
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow(() -> new ItemNotFoundException("Product not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Product> findAll() {
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product save(Product product) {
|
||||||
|
if (StringUtils.isEmpty(product.getName()) || product.getPrice() == 0.0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
int newId = products.stream()
|
||||||
|
.mapToInt(Product::getId)
|
||||||
|
.max()
|
||||||
|
.getAsInt() + 1;
|
||||||
|
product.setId(newId);
|
||||||
|
products.add(product);
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ItemNotFoundException extends RuntimeException {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public ItemNotFoundException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
spring.main.allow-bean-definition-overriding=true
|
13
spring-boot-mvc-2/src/main/resources/logback.xml
Normal file
13
spring-boot-mvc-2/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user