BAEL-20869 Move remaining spring boot modules
This commit is contained in:
@@ -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/
|
||||
@@ -0,0 +1,21 @@
|
||||
## Spring Boot MVC
|
||||
|
||||
This module contains articles about Spring Web MVC in Spring Boot projects.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon)
|
||||
- [Custom Validation MessageSource in Spring Boot](https://www.baeldung.com/spring-custom-validation-message-source)
|
||||
- [Spring Boot Annotations](https://www.baeldung.com/spring-boot-annotations)
|
||||
- [Spring Scheduling Annotations](https://www.baeldung.com/spring-scheduling-annotations)
|
||||
- [Spring Web Annotations](https://www.baeldung.com/spring-mvc-annotations)
|
||||
- [Spring Core Annotations](https://www.baeldung.com/spring-core-annotations)
|
||||
- [Display RSS Feed with Spring MVC](https://www.baeldung.com/spring-mvc-rss-feed)
|
||||
- [A Controller, Service and DAO Example with Spring Boot and JSF](https://www.baeldung.com/jsf-spring-boot-controller-service-dao)
|
||||
- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache)
|
||||
- [Setting Up Swagger 2 with a Spring REST API](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
|
||||
- [Conditionally Enable Scheduled Jobs in Spring](https://www.baeldung.com/spring-scheduled-enabled-conditionally)
|
||||
- [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js)
|
||||
- [Using Spring ResponseEntity to Manipulate the HTTP Response](https://www.baeldung.com/spring-response-entity)
|
||||
- [Spring Bean Annotations](https://www.baeldung.com/spring-bean-annotations)
|
||||
- More articles: [[next -->]](/spring-boot-mvc-2)
|
||||
@@ -0,0 +1,156 @@
|
||||
<?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</artifactId>
|
||||
<name>spring-boot-mvc</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Module For Spring Boot MVC</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<!-- Snapshot repository location -->
|
||||
<repository>
|
||||
<id>jcenter-snapshots</id>
|
||||
<name>jcenter</name>
|
||||
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-jasper</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!--JSF -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.faces</artifactId>
|
||||
<version>${javax.faces.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- ROME for RSS -->
|
||||
<dependency>
|
||||
<groupId>com.rometools</groupId>
|
||||
<artifactId>rome</artifactId>
|
||||
<version>${rome.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--Validation -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Fox 2 -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-data-rest</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-bean-validators</artifactId>
|
||||
<version>${spring.fox.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- AOP -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.9.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.9.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.springbootmvc.SpringBootMvcApplication</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring.fox.version>3.0.0-SNAPSHOT</spring.fox.version>
|
||||
<!-- ROME for RSS -->
|
||||
<rome.version>1.10.0</rome.version>
|
||||
<javax.faces.version>2.3.7</javax.faces.version>
|
||||
<start-class>com.baeldung.springbootmvc.SpringBootMvcApplication</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.accessparamsjs;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* Sample rest controller for the tutorial article
|
||||
* "Access Spring MVC Model object in JavaScript".
|
||||
*
|
||||
* @author Andrew Shcherbakov
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class Controller {
|
||||
|
||||
/**
|
||||
* Define two model objects (one integer and one string) and pass them to the view.
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/index")
|
||||
public ModelAndView thymeleafView(Map<String, Object> model) {
|
||||
model.put("number", 1234);
|
||||
model.put("message", "Hello from Spring MVC");
|
||||
return new ModelAndView("thymeleaf/index");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Required;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
|
||||
@DependsOn
|
||||
public class Bike implements Vehicle {
|
||||
|
||||
private String color;
|
||||
|
||||
@Required
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Biker {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("bike")
|
||||
private Vehicle vehicle;
|
||||
|
||||
@Autowired
|
||||
public Biker(@Qualifier("bike") Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setVehicle(@Qualifier("bike") Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
@DependsOn("engine")
|
||||
public class Car implements Vehicle {
|
||||
|
||||
@Autowired
|
||||
private Engine engine;
|
||||
|
||||
@Autowired
|
||||
public Car(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setEngine(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Lazy
|
||||
public class CarMechanic {
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CarUtility {
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
|
||||
public class CustomException extends RuntimeException {
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Driver {
|
||||
|
||||
@Autowired
|
||||
private Vehicle vehicle;
|
||||
|
||||
@Autowired
|
||||
public Driver(Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setVehicle(Vehicle vehicle) {
|
||||
this.vehicle = vehicle;
|
||||
}
|
||||
|
||||
public Vehicle getVehicle() {
|
||||
return vehicle;
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = 10000)
|
||||
@Scheduled(cron = "0 * * * * MON-FRI")
|
||||
public void checkVehicle() {
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
public class Engine {
|
||||
|
||||
@Value("8")
|
||||
private int cylinderCount;
|
||||
|
||||
@Value("${engine.fuelType}")
|
||||
private String fuelType;
|
||||
|
||||
public Engine() {
|
||||
this(8);
|
||||
}
|
||||
|
||||
public Engine(@Value("8") int cylinderCount) {
|
||||
this.cylinderCount = cylinderCount;
|
||||
}
|
||||
|
||||
@Value("8")
|
||||
public void setCylinderCount(int cylinderCount) {
|
||||
this.cylinderCount = cylinderCount;
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class PerformanceAspect {
|
||||
|
||||
private static Logger logger = Logger.getLogger(PerformanceAspect.class.getName());
|
||||
|
||||
@Pointcut("within(@org.springframework.stereotype.Repository *)")
|
||||
public void repositoryClassMethods() {
|
||||
}
|
||||
|
||||
@Around("repositoryClassMethods()")
|
||||
public Object measureMethodExecutionTime(ProceedingJoinPoint pjp) throws Throwable {
|
||||
long start = System.nanoTime();
|
||||
Object retval = pjp.proceed();
|
||||
long end = System.nanoTime();
|
||||
String methodName = pjp.getSignature().getName();
|
||||
logger.info("Execution of " + methodName + " took " + TimeUnit.NANOSECONDS.toMillis(end - start) + " ms");
|
||||
return retval;
|
||||
}
|
||||
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
public interface Vehicle {
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@Controller
|
||||
@RequestMapping(value = "/vehicles", method = RequestMethod.GET)
|
||||
public class VehicleController {
|
||||
|
||||
@CrossOrigin
|
||||
@ResponseBody
|
||||
@RequestMapping("/hello")
|
||||
public String hello() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
@RequestMapping("/home")
|
||||
public String home() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public void saveVehicle(@RequestBody Vehicle vehicle) {
|
||||
}
|
||||
|
||||
@RequestMapping("/{id}")
|
||||
public Vehicle getVehicle(@PathVariable("id") long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping
|
||||
public Vehicle getVehicleByParam(@RequestParam("id") long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@RequestMapping("/buy")
|
||||
public Car buyCar(@RequestParam(defaultValue = "5") int seatCount) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public void onIllegalArgumentException(IllegalArgumentException exception) {
|
||||
}
|
||||
|
||||
@PostMapping("/assemble")
|
||||
public void assembleVehicle(@ModelAttribute("vehicle") Vehicle vehicle) {
|
||||
}
|
||||
|
||||
@ModelAttribute("vehicle")
|
||||
public Vehicle getVehicle() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class VehicleFactoryApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(VehicleFactoryApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.annotations")
|
||||
@ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
|
||||
@ImportResource("classpath:/annotations.xml")
|
||||
@PropertySource("classpath:/annotations.properties")
|
||||
@Lazy
|
||||
@EnableAutoConfiguration
|
||||
@EnableAsync
|
||||
@EnableScheduling
|
||||
public class VehicleFactoryConfig {
|
||||
|
||||
@Bean
|
||||
@Lazy(false)
|
||||
public Engine engine() {
|
||||
return new Engine();
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class VehicleRepository {
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class VehicleRestController {
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class VehicleService {
|
||||
|
||||
@Async
|
||||
public void repairCar() {
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.nosuchbeandefinitionexception;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BeanA {
|
||||
|
||||
@Autowired
|
||||
BeanB dependency;
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.nosuchbeandefinitionexception;
|
||||
|
||||
public class BeanB {
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.nosuchbeandefinitionexception;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class NoSuchBeanDefinitionDemoApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NoSuchBeanDefinitionDemoApp.class, args);
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.responseentity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Year;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/customResponse")
|
||||
public class CustomResponseController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
public ResponseEntity<String> hello() {
|
||||
return new ResponseEntity<>("Hello World!", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/age")
|
||||
public ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
|
||||
if (isInFuture(yearOfBirth)) {
|
||||
return new ResponseEntity<>("Year of birth cannot be in the future", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>("Your age is " + calculateAge(yearOfBirth), HttpStatus.OK);
|
||||
}
|
||||
|
||||
private int calculateAge(int yearOfBirth) {
|
||||
return currentYear() - yearOfBirth;
|
||||
}
|
||||
|
||||
private boolean isInFuture(int year) {
|
||||
return currentYear() < year;
|
||||
}
|
||||
|
||||
private int currentYear() {
|
||||
return Year.now().getValue();
|
||||
}
|
||||
|
||||
@GetMapping("/customHeader")
|
||||
public ResponseEntity<String> customHeader() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Custom-Header", "foo");
|
||||
|
||||
return new ResponseEntity<>("Custom header set", headers, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/manual")
|
||||
public void manual(HttpServletResponse response) throws IOException {
|
||||
response.setHeader("Custom-Header", "foo");
|
||||
response.setStatus(200);
|
||||
response.getWriter()
|
||||
.println("Hello World!");
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.responseentity;
|
||||
|
||||
import java.time.Year;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/customResponseWithBuilder")
|
||||
public class CustomResponseWithBuilderController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
public ResponseEntity<String> hello() {
|
||||
return ResponseEntity.ok("Hello World!");
|
||||
}
|
||||
|
||||
@GetMapping("/age")
|
||||
public ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
|
||||
if (isInFuture(yearOfBirth)) {
|
||||
return ResponseEntity.badRequest()
|
||||
.body("Year of birth cannot be in the future");
|
||||
}
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK)
|
||||
.body("Your age is " + calculateAge(yearOfBirth));
|
||||
}
|
||||
|
||||
private int calculateAge(int yearOfBirth) {
|
||||
return currentYear() - yearOfBirth;
|
||||
}
|
||||
|
||||
private boolean isInFuture(int year) {
|
||||
return currentYear() < year;
|
||||
}
|
||||
|
||||
private int currentYear() {
|
||||
return Year.now()
|
||||
.getValue();
|
||||
}
|
||||
|
||||
@GetMapping("/customHeader")
|
||||
public ResponseEntity<String> customHeader() {
|
||||
return ResponseEntity.ok()
|
||||
.header("Custom-Header", "foo")
|
||||
.body("Custom header set");
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.rss;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot launcher for an application which exposes an RSS Feed.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class RssFeedApplication {
|
||||
|
||||
/**
|
||||
* Launches a Spring Boot application which exposes an RSS Feed.
|
||||
*
|
||||
* @param args null
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(RssFeedApplication.class, args);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.rss;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.View;
|
||||
|
||||
/**
|
||||
* REST Controller which returns an RSS Feed created by {@link RssFeedView}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class RssFeedController {
|
||||
|
||||
/**
|
||||
* View used by this controller.
|
||||
*/
|
||||
@Autowired
|
||||
private RssFeedView view;
|
||||
|
||||
/**
|
||||
* Returns an RSS Feed created by {@link #view}.
|
||||
*
|
||||
* @return an RSS Feed
|
||||
*/
|
||||
@GetMapping("/rss")
|
||||
public View getFeed() {
|
||||
return view;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.baeldung.rss;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
|
||||
|
||||
import com.rometools.rome.feed.rss.Channel;
|
||||
import com.rometools.rome.feed.rss.Item;
|
||||
|
||||
/**
|
||||
* View for a RSS feed.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
@Component
|
||||
public class RssFeedView extends AbstractRssFeedView {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.feed.AbstractFeedView#
|
||||
* buildFeedMetadata(java.util.Map, com.rometools.rome.feed.WireFeed,
|
||||
* javax.servlet.http.HttpServletRequest)
|
||||
*/
|
||||
@Override
|
||||
protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) {
|
||||
feed.setTitle("Baeldung RSS Feed");
|
||||
feed.setDescription("Learn how to program in Java");
|
||||
feed.setLink("http://www.baeldung.com");
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.feed.AbstractRssFeedView#
|
||||
* buildFeedItems(java.util.Map, javax.servlet.http.HttpServletRequest,
|
||||
* javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
protected List<Item> buildFeedItems(Map<String, Object> model, HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
|
||||
// Builds the single entries.
|
||||
Item entryOne = new Item();
|
||||
entryOne.setTitle("JUnit 5 @Test Annotation");
|
||||
entryOne.setAuthor("[email protected]");
|
||||
entryOne.setLink("http://www.baeldung.com/junit-5-test-annotation");
|
||||
entryOne.setPubDate(Date.from(Instant.parse("2017-12-19T00:00:00Z")));
|
||||
|
||||
Item entryTwo = new Item();
|
||||
entryTwo.setTitle("Creating and Configuring Jetty 9 Server in Java");
|
||||
entryTwo.setAuthor("[email protected]");
|
||||
entryTwo.setLink("http://www.baeldung.com/jetty-java-programmatic");
|
||||
entryTwo.setPubDate(Date.from(Instant.parse("2018-01-23T00:00:00Z")));
|
||||
|
||||
Item entryThree = new Item();
|
||||
entryThree.setTitle("Flyweight Pattern in Java");
|
||||
entryThree.setAuthor("[email protected]");
|
||||
entryThree.setLink("http://www.baeldung.com/java-flyweight");
|
||||
entryThree.setPubDate(Date.from(Instant.parse("2018-02-01T00:00:00Z")));
|
||||
|
||||
Item entryFour = new Item();
|
||||
entryFour.setTitle("Multi-Swarm Optimization Algorithm in Java");
|
||||
entryFour.setAuthor("[email protected]");
|
||||
entryFour.setLink("http://www.baeldung.com/java-multi-swarm-algorithm");
|
||||
entryFour.setPubDate(Date.from(Instant.parse("2018-03-09T00:00:00Z")));
|
||||
|
||||
Item entryFive = new Item();
|
||||
entryFive.setTitle("A Simple Tagging Implementation with MongoDB");
|
||||
entryFive.setAuthor("[email protected]");
|
||||
entryFive.setLink("http://www.baeldung.com/mongodb-tagging");
|
||||
entryFive.setPubDate(Date.from(Instant.parse("2018-03-27T00:00:00Z")));
|
||||
|
||||
Item entrySix = new Item();
|
||||
entrySix.setTitle("Double-Checked Locking with Singleton");
|
||||
entrySix.setAuthor("[email protected]");
|
||||
entrySix.setLink("http://www.baeldung.com/java-singleton-double-checked-locking");
|
||||
entrySix.setPubDate(Date.from(Instant.parse("2018-04-23T00:00:00Z")));
|
||||
|
||||
Item entrySeven = new Item();
|
||||
entrySeven.setTitle("Introduction to Dagger 2");
|
||||
entrySeven.setAuthor("[email protected]");
|
||||
entrySeven.setLink("http://www.baeldung.com/dagger-2");
|
||||
entrySeven.setPubDate(Date.from(Instant.parse("2018-06-30T00:00:00Z")));
|
||||
|
||||
// Creates the feed.
|
||||
return Arrays.asList(entryOne, entryTwo, entryThree, entryFour, entryFive, entrySix, entrySeven);
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.scheduling;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
@Configuration
|
||||
public class ScheduleJobsByProfile {
|
||||
|
||||
private final static Logger LOG = LoggerFactory.getLogger(ScheduleJobsByProfile.class);
|
||||
|
||||
@Profile("prod")
|
||||
@Bean
|
||||
public ScheduledJob scheduledJob()
|
||||
{
|
||||
return new ScheduledJob("@Profile");
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user