Merge branch 'master' into JAVA-3524
This commit is contained in:
@@ -23,7 +23,10 @@
|
||||
<module>spring-mvc-forms-thymeleaf</module>
|
||||
<module>spring-mvc-java</module>
|
||||
<module>spring-mvc-java-2</module>
|
||||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-thymeleaf-2</module>
|
||||
<module>spring-thymeleaf-3</module>
|
||||
|
||||
@@ -78,12 +78,6 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
<version>${springcloud.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
@@ -149,7 +143,6 @@
|
||||
<jpa.version>2.2</jpa.version>
|
||||
<guava.version>18.0</guava.version>
|
||||
<subethasmtp.version>3.1.7</subethasmtp.version>
|
||||
<springcloud.version>2.0.2.RELEASE</springcloud.version>
|
||||
<httpclient.version>4.5.8</httpclient.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -5,3 +5,6 @@ spring.mvc.pathmatch.use-suffix-pattern=true
|
||||
#spring.mvc.contentnegotiation.favor-path-extension=true
|
||||
#spring.mvc.contentnegotiation.favor-parameter=true
|
||||
#spring.mvc.contentnegotiation.parameter-name=mediaType
|
||||
|
||||
# https://github.com/spring-projects/spring-boot/issues/24207
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
## Spring MVC WebFlow
|
||||
|
||||
This module contains articles about Spring MVC Web Flow
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Guide to Spring Web Flow](https://www.baeldung.com/spring-web-flow)
|
||||
@@ -0,0 +1,120 @@
|
||||
<?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-mvc-webflow</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-mvc-webflow</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Web Flow -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.webflow</groupId>
|
||||
<artifactId>spring-webflow</artifactId>
|
||||
<version>${spring.webflow}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot-starter-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-mvc-webflow</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.tomee.maven</groupId>
|
||||
<artifactId>tomee-maven-plugin</artifactId>
|
||||
<version>8.0.1</version>
|
||||
<configuration>
|
||||
<tomeeHttpPort>8080</tomeeHttpPort>
|
||||
<context>spring-mvc-webflow</context>
|
||||
<reloadOnUpdate>true</reloadOnUpdate>
|
||||
<tomeeClassifier>plume</tomeeClassifier>
|
||||
<synchronization>
|
||||
<extensions>
|
||||
<extension>.class</extension>
|
||||
</extensions>
|
||||
</synchronization>
|
||||
<args>-Xmx2048m -XX:PermSize=256m -Dtomee.serialization.class.blacklist=- -Dtomee.serialization.class.whitelist=*</args>
|
||||
<removeDefaultWebapps>true</removeDefaultWebapps>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.0.1.RELEASE</org.springframework.version>
|
||||
|
||||
<!-- Spring Web Flow -->
|
||||
<spring.webflow>2.5.0.RELEASE</spring.webflow>
|
||||
|
||||
<httpcore.version>4.4.5</httpcore.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.servlet;
|
||||
|
||||
import javax.servlet.ServletRegistration.Dynamic;
|
||||
|
||||
import com.baeldung.spring.WebFlowConfig;
|
||||
import com.baeldung.spring.WebMvcConfig;
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
public WebInitializer() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class<?>[] { WebMvcConfig.class, WebFlowConfig.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeRegistration(final Dynamic registration) {
|
||||
super.customizeRegistration(registration);
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.webflow.config.AbstractFlowConfiguration;
|
||||
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
|
||||
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;
|
||||
import org.springframework.webflow.executor.FlowExecutor;
|
||||
import org.springframework.webflow.mvc.builder.MvcViewFactoryCreator;
|
||||
|
||||
@Configuration
|
||||
public class WebFlowConfig extends AbstractFlowConfiguration {
|
||||
|
||||
@Autowired
|
||||
private WebMvcConfig webMvcConfig;
|
||||
|
||||
@Bean
|
||||
public FlowDefinitionRegistry flowRegistry() {
|
||||
return getFlowDefinitionRegistryBuilder(flowBuilderServices()).addFlowLocation("/WEB-INF/flows/activation-flow.xml", "activationFlow").build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FlowExecutor flowExecutor() {
|
||||
return getFlowExecutorBuilder(flowRegistry()).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FlowBuilderServices flowBuilderServices() {
|
||||
return getFlowBuilderServicesBuilder().setViewFactoryCreator(mvcViewFactoryCreator()).setDevelopmentMode(true).build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MvcViewFactoryCreator mvcViewFactoryCreator() {
|
||||
MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
|
||||
factoryCreator.setViewResolvers(Collections.singletonList(this.webMvcConfig.viewResolver()));
|
||||
factoryCreator.setUseSpringBeanBinding(true);
|
||||
return factoryCreator;
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
import org.springframework.webflow.mvc.servlet.FlowHandlerAdapter;
|
||||
import org.springframework.webflow.mvc.servlet.FlowHandlerMapping;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Autowired
|
||||
private WebFlowConfig webFlowConfig;
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public InternalResourceViewResolver viewResolver() {
|
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
|
||||
viewResolver.setViewClass(JstlView.class);
|
||||
viewResolver.setPrefix("/WEB-INF/view/");
|
||||
viewResolver.setSuffix(".jsp");
|
||||
return viewResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FlowHandlerMapping flowHandlerMapping() {
|
||||
FlowHandlerMapping handlerMapping = new FlowHandlerMapping();
|
||||
handlerMapping.setOrder(-1);
|
||||
handlerMapping.setFlowRegistry(this.webFlowConfig.flowRegistry());
|
||||
return handlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FlowHandlerAdapter flowHandlerAdapter() {
|
||||
FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter();
|
||||
handlerAdapter.setFlowExecutor(this.webFlowConfig.flowExecutor());
|
||||
handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true);
|
||||
return handlerAdapter;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:flow="http://www.springframework.org/schema/webflow-config"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/webflow-config
|
||||
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.4.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
|
||||
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
|
||||
<property name="flowRegistry" ref="activationFlowRegistry"/>
|
||||
</bean>
|
||||
|
||||
<flow:flow-builder-services id="flowBuilderServices"
|
||||
view-factory-creator="mvcViewFactoryCreator"/>
|
||||
|
||||
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
|
||||
<property name="viewResolvers" ref="jspViewResolver"/>
|
||||
</bean>
|
||||
|
||||
<flow:flow-registry id="activationFlowRegistry" flow-builder-services="flowBuilderServices">
|
||||
<flow:flow-location id="activationFlow" path="/WEB-INF/flows/activation-flow.xml"/>
|
||||
</flow:flow-registry>
|
||||
|
||||
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
|
||||
<property name="flowExecutor" ref="activationFlowExecutor"/>
|
||||
</bean>
|
||||
<flow:flow-executor id="activationFlowExecutor" flow-registry="activationFlowRegistry"/>
|
||||
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<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>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<flow xmlns="http://www.springframework.org/schema/webflow"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/webflow
|
||||
http://www.springframework.org/schema/webflow/spring-webflow.xsd">
|
||||
|
||||
|
||||
<view-state id="activation">
|
||||
<transition on="activate" to="success"/>
|
||||
<transition on="cancel" to="failure"/>
|
||||
</view-state>
|
||||
|
||||
<view-state id="success" />
|
||||
|
||||
<view-state id="failure" />
|
||||
|
||||
</flow>
|
||||
@@ -0,0 +1,23 @@
|
||||
<html>
|
||||
<body>
|
||||
<h2>Hello World!</h2>
|
||||
<%--<a href="${flowExecutionUrl}">Start</a>--%>
|
||||
<%--<input type="submit" name="_eventId_success" value="Proceed" />--%>
|
||||
<%--<input type="submit" name="_eventId_failure" value="Cancel" />--%>
|
||||
|
||||
<form method="post" action="${flowExecutionUrl}">
|
||||
|
||||
<input type="hidden" name="_eventId" value="activate">
|
||||
<input type="submit" value="Proceed" />
|
||||
|
||||
</form>
|
||||
|
||||
<form method="post" action="${flowExecutionUrl}">
|
||||
|
||||
<input type="hidden" name="_eventId" value="cancel">
|
||||
<input type="submit" value="Cancel" />
|
||||
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<h2>Activation Failed</h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>This is the body of the sample view</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<h2>Activation Successful!</h2>
|
||||
</body>
|
||||
</html>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.spring.WebFlowConfig;
|
||||
import com.baeldung.spring.WebMvcConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = {WebFlowConfig.class, WebMvcConfig.class})
|
||||
@WebAppConfiguration
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
## Spring REST Angular
|
||||
|
||||
This module contains articles about REST APIs with Spring and Angular
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Pagination with Spring REST and AngularJS table](https://www.baeldung.com/pagination-with-a-spring-rest-api-and-an-angularjs-table)
|
||||
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-rest-angular</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>spring-rest-angular</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>spring-mock-mvc</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-rest-angular</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<guava.version>19.0</guava.version>
|
||||
<start-class>com.baeldung.web.main.Application</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.web.dao;
|
||||
|
||||
import com.baeldung.web.entity.Student;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StudentRepository extends JpaRepository<Student, Long>
|
||||
{
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.web.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Student implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(long id, String name, String gender, Integer age) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.gender = gender;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
@Column(nullable = false)
|
||||
private String gender;
|
||||
@Column(nullable = false)
|
||||
private Integer age;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
public class MyResourceNotFoundException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 4088649120307193208L;
|
||||
|
||||
public MyResourceNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MyResourceNotFoundException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public MyResourceNotFoundException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MyResourceNotFoundException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.web.main;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.web.filter.ShallowEtagHeaderFilter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAutoConfiguration
|
||||
@Import(PersistenceConfig.class)
|
||||
public class Application implements WebMvcConfigurer {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
|
||||
return new ShallowEtagHeaderFilter();
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.web.main;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@EnableJpaRepositories("com.baeldung.web.dao")
|
||||
@ComponentScan(basePackages = { "com.baeldung.web" })
|
||||
@EntityScan("com.baeldung.web.entity")
|
||||
@Configuration
|
||||
public class PersistenceConfig {
|
||||
|
||||
@Bean
|
||||
public JdbcTemplate getJdbcTemplate() {
|
||||
return new JdbcTemplate(dataSource());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).build();
|
||||
return db;
|
||||
}
|
||||
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.web.rest;
|
||||
|
||||
import com.baeldung.web.entity.Student;
|
||||
import com.baeldung.web.exception.MyResourceNotFoundException;
|
||||
import com.baeldung.web.service.StudentService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
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.RestController;
|
||||
|
||||
@RestController
|
||||
public class StudentDirectoryRestController {
|
||||
|
||||
@Autowired
|
||||
private StudentService service;
|
||||
|
||||
@RequestMapping(value = "/student/get", params = { "page", "size" }, method = RequestMethod.GET, produces = "application/json")
|
||||
public Page<Student> findPaginated(@RequestParam("page") int page, @RequestParam("size") int size) {
|
||||
|
||||
Page<Student> resultPage = service.findPaginated(page, size);
|
||||
if (page > resultPage.getTotalPages()) {
|
||||
throw new MyResourceNotFoundException();
|
||||
}
|
||||
return resultPage;
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.web.service;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public interface IOperations<T> {
|
||||
|
||||
public Page<T> findPaginated(final int page, final int size);
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.web.service;
|
||||
|
||||
import com.baeldung.web.entity.Student;
|
||||
|
||||
public interface StudentService extends IOperations<Student> {
|
||||
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.web.service;
|
||||
|
||||
import com.baeldung.web.dao.StudentRepository;
|
||||
import com.baeldung.web.entity.Student;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class StudentServiceImpl implements StudentService {
|
||||
|
||||
@Autowired
|
||||
private StudentRepository dao;
|
||||
|
||||
@Override
|
||||
public Page<Student> findPaginated(int page, int size) {
|
||||
return dao.findAll(PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
server.servlet.contextPath=/
|
||||
spring.h2.console.enabled=true
|
||||
logging.level.org.hibernate.SQL=info
|
||||
spring.jpa.generate-ddl=true
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
@@ -0,0 +1,40 @@
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (1,'Bryan', 'Male',20);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (2, 'Ben', 'Male', 22);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (3,'Lisa', 'Female',24);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (4,'Sarah', 'Female',20);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (5,'Jay', 'Male',20);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (6,'John', 'Male',22);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (7,'Jordan', 'Male',24);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (8,'Rob', 'Male',26);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (9,'Will', 'Male',20);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (10,'Shawn', 'Male',22);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (11,'Taylor', 'Female',24);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (12,'Venus', 'Female',26);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (13,'Vince', 'Male',20);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (14,'Carol', 'Female',22);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (15,'Joana', 'Female',24);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (16,'Dion', 'Male',26);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (17,'Evans', 'Male',20);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (18,'Bart', 'Male',22);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (19,'Jenny', 'Female',24);
|
||||
INSERT INTO student (id,name,gender,age)
|
||||
VALUES (20,'Kristine', 'Female',26);
|
||||
@@ -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>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user