From be0528bdcb8cf0b0f12ab5f068928b2e6f699182 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 28 Sep 2021 18:11:14 +0100 Subject: [PATCH] Rework the samples to avoid depending on Spring Boot Closes gh-711 --- samples/junit5/build.gradle | 27 ++--- .../junit5/SampleJUnit5Application.java | 11 +- .../junit5/SampleJUnit5ApplicationTests.java | 8 +- samples/rest-notes-slate/build.gradle | 32 +++--- .../com/example/notes/ErrorController.java | 45 ++++++++ .../src/main/java/com/example/notes/Note.java | 12 +- .../com/example/notes/RestNotesSlate.java | 58 ++++++++-- .../src/main/java/com/example/notes/Tag.java | 12 +- .../src/main/resources/application.properties | 1 - .../com/example/notes/ApiDocumentation.java | 12 +- samples/rest-notes-spring-data-rest/pom.xml | 105 ++++++++++++------ .../com/example/notes/ErrorController.java | 45 ++++++++ .../src/main/java/com/example/notes/Note.java | 12 +- .../notes/RestNotesSpringDataRest.java | 60 ++++++++-- .../src/main/java/com/example/notes/Tag.java | 12 +- .../com/example/notes/ApiDocumentation.java | 8 +- .../notes/GettingStartedDocumentation.java | 10 +- .../rest-notes-spring-hateoas/build.gradle | 39 +++---- .../com/example/notes/ErrorController.java | 45 ++++++++ .../ExceptionSupressingErrorAttributes.java | 41 ------- .../src/main/java/com/example/notes/Note.java | 10 +- .../example/notes/RestNotesSpringHateoas.java | 58 ++++++++-- .../src/main/java/com/example/notes/Tag.java | 12 +- .../com/example/notes/ApiDocumentation.java | 10 +- .../notes/GettingStartedDocumentation.java | 12 +- .../example/notes/NullOrNotBlankTests.java | 3 +- samples/testng/build.gradle | 22 ++-- .../testng/SampleTestNgApplication.java | 11 +- .../testng/SampleTestNgApplicationTests.java | 8 +- samples/web-test-client/build.gradle | 17 +-- .../SampleWebTestClientApplication.java | 15 +-- 31 files changed, 505 insertions(+), 268 deletions(-) create mode 100644 samples/rest-notes-slate/src/main/java/com/example/notes/ErrorController.java delete mode 100644 samples/rest-notes-slate/src/main/resources/application.properties create mode 100644 samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/ErrorController.java create mode 100644 samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ErrorController.java delete mode 100644 samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ExceptionSupressingErrorAttributes.java diff --git a/samples/junit5/build.gradle b/samples/junit5/build.gradle index cb3c7044..284e7833 100644 --- a/samples/junit5/build.gradle +++ b/samples/junit5/build.gradle @@ -2,11 +2,8 @@ plugins { id "eclipse" id "java" id "org.asciidoctor.jvm.convert" version "3.3.2" - id "org.springframework.boot" version "2.4.7" } -apply plugin: 'io.spring.dependency-management' - repositories { mavenLocal() maven { url 'https://repo.spring.io/milestone' } @@ -20,26 +17,23 @@ sourceCompatibility = 17 targetCompatibility = 17 ext { + restdocsVersion = '3.0.0-SNAPSHOT' snippetsDir = file('build/generated-snippets') } -ext['spring-restdocs.version'] = '3.0.0-SNAPSHOT' - configurations { asciidoctorExtensions } dependencies { - asciidoctorExtensions 'org.springframework.restdocs:spring-restdocs-asciidoctor' + asciidoctorExtensions "org.springframework.restdocs:spring-restdocs-asciidoctor:$restdocsVersion" - implementation 'org.springframework.boot:spring-boot-starter-web' - - testImplementation('org.springframework.boot:spring-boot-starter-test') { - exclude group: 'junit', module: 'junit;' - } - testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' - testImplementation 'org.junit.jupiter:junit-jupiter-api' - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine' + implementation 'org.springframework:spring-webmvc:6.0.0-SNAPSHOT' + + testImplementation "org.springframework.restdocs:spring-restdocs-mockmvc:$restdocsVersion" + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.0' + + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.0' } test { @@ -53,12 +47,9 @@ asciidoctor { dependsOn test } -bootJar { +jar { dependsOn asciidoctor from ("${asciidoctor.outputDir}/html5") { into 'static/docs' } } - -eclipseJdt.onlyIf { false } -cleanEclipseJdt.onlyIf { false } diff --git a/samples/junit5/src/main/java/com/example/junit5/SampleJUnit5Application.java b/samples/junit5/src/main/java/com/example/junit5/SampleJUnit5Application.java index fec007f6..3f2d403b 100644 --- a/samples/junit5/src/main/java/com/example/junit5/SampleJUnit5Application.java +++ b/samples/junit5/src/main/java/com/example/junit5/SampleJUnit5Application.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,13 @@ package com.example.junit5; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -@SpringBootApplication +@Configuration public class SampleJUnit5Application { - public static void main(String[] args) { - new SpringApplication(SampleJUnit5Application.class).run(args); - } - @RestController private static class SampleController { diff --git a/samples/junit5/src/test/java/com/example/junit5/SampleJUnit5ApplicationTests.java b/samples/junit5/src/test/java/com/example/junit5/SampleJUnit5ApplicationTests.java index a6e4169d..99ddd81b 100644 --- a/samples/junit5/src/test/java/com/example/junit5/SampleJUnit5ApplicationTests.java +++ b/samples/junit5/src/test/java/com/example/junit5/SampleJUnit5ApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,15 +25,17 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -@SpringBootTest +@WebAppConfiguration +@ContextConfiguration(classes = SampleJUnit5Application.class) @ExtendWith({RestDocumentationExtension.class, SpringExtension.class}) public class SampleJUnit5ApplicationTests { diff --git a/samples/rest-notes-slate/build.gradle b/samples/rest-notes-slate/build.gradle index 7b7648c1..45ac6a2e 100644 --- a/samples/rest-notes-slate/build.gradle +++ b/samples/rest-notes-slate/build.gradle @@ -1,11 +1,7 @@ plugins { - id "eclipse" id "java" - id "org.springframework.boot" version "2.4.7" } -apply plugin: 'io.spring.dependency-management' - repositories { mavenLocal() maven { url 'https://repo.spring.io/milestone' } @@ -19,24 +15,31 @@ sourceCompatibility = 17 targetCompatibility = 17 ext { + restdocsVersion = '3.0.0-SNAPSHOT' snippetsDir = file('build/generated-snippets') } ext['spring-restdocs.version'] = '3.0.0-SNAPSHOT' dependencies { - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation 'org.springframework.boot:spring-boot-starter-data-rest' + implementation "com.fasterxml.jackson.core:jackson-databind:2.12.5" + implementation "jakarta.servlet:jakarta.servlet-api:5.0.0" + implementation "org.hibernate.validator:hibernate-validator:7.0.0.Final" + implementation "org.hibernate:hibernate-core-jakarta:5.5.7.Final" + implementation "org.springframework:spring-webmvc:6.0.0-SNAPSHOT" + implementation "org.springframework.data:spring-data-jpa:3.0.0-SNAPSHOT" + implementation "org.springframework.data:spring-data-rest-webmvc:4.0.0-SNAPSHOT" - runtimeOnly 'com.h2database:h2' + runtimeOnly 'com.h2database:h2:1.4.200' runtimeOnly 'org.atteo:evo-inflector:1.2.1' - testImplementation 'com.jayway.jsonpath:json-path' - testImplementation('org.junit.vintage:junit-vintage-engine') { - exclude group: 'org.hamcrest', module: 'hamcrest-core' - } - testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' + testImplementation 'com.jayway.jsonpath:json-path:2.6.0' + testImplementation 'junit:junit:4.12' + testImplementation 'org.assertj:assertj-core:3.21.0' + testImplementation 'org.hamcrest:hamcrest-library:2.2' + testImplementation "org.springframework.restdocs:spring-restdocs-mockmvc:$restdocsVersion" + + testRuntimeOnly 'org.glassfish:jakarta.el:4.0.2' } test { @@ -59,6 +62,3 @@ task(slate, type: Exec) { build { dependsOn 'slate' } - -eclipseJdt.onlyIf { false } -cleanEclipseJdt.onlyIf { false } diff --git a/samples/rest-notes-slate/src/main/java/com/example/notes/ErrorController.java b/samples/rest-notes-slate/src/main/java/com/example/notes/ErrorController.java new file mode 100644 index 00000000..557090e0 --- /dev/null +++ b/samples/rest-notes-slate/src/main/java/com/example/notes/ErrorController.java @@ -0,0 +1,45 @@ +/* + * Copyright 2014-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.notes; + +import java.time.Instant; +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.http.HttpServletRequest; + +@RestController +class ErrorController { + + @RequestMapping("/error") + ResponseEntity> handleError(HttpServletRequest request) { + HttpStatus httpStatus = HttpStatus.valueOf((int)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)); + Map body = Map.of("status", httpStatus.value(), + "error", httpStatus.getReasonPhrase(), + "path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), + "message", request.getAttribute(RequestDispatcher.ERROR_MESSAGE), + "timestamp", Instant.now().toEpochMilli()); + ResponseEntity> response = new ResponseEntity<>(body, httpStatus); + return response; + } + +} diff --git a/samples/rest-notes-slate/src/main/java/com/example/notes/Note.java b/samples/rest-notes-slate/src/main/java/com/example/notes/Note.java index 53104ede..e2926477 100644 --- a/samples/rest-notes-slate/src/main/java/com/example/notes/Note.java +++ b/samples/rest-notes-slate/src/main/java/com/example/notes/Note.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,11 @@ package com.example.notes; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/samples/rest-notes-slate/src/main/java/com/example/notes/RestNotesSlate.java b/samples/rest-notes-slate/src/main/java/com/example/notes/RestNotesSlate.java index e20e4fec..afd1bc97 100644 --- a/samples/rest-notes-slate/src/main/java/com/example/notes/RestNotesSlate.java +++ b/samples/rest-notes-slate/src/main/java/com/example/notes/RestNotesSlate.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,58 @@ package com.example.notes; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import javax.sql.DataSource; -@SpringBootApplication +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import jakarta.persistence.EntityManagerFactory; + +@EnableWebMvc +@ComponentScan +@Configuration +@EnableJpaRepositories +@Import(RepositoryRestMvcConfiguration.class) public class RestNotesSlate { - - public static void main(String[] args) { - SpringApplication.run(RestNotesSlate.class, args); + + @Bean + ObjectMapper objectMapper() { + return Jackson2ObjectMapperBuilder.json().build(); + } + + @Bean + LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); + factory.setPackagesToScan("com.example.notes"); + HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); + jpaVendorAdapter.setShowSql(true); + jpaVendorAdapter.setGenerateDdl(true); + factory.setJpaVendorAdapter(jpaVendorAdapter); + factory.setDataSource(dataSource); + return factory; + } + + @Bean + DataSource dataSource() { + return new DriverManagerDataSource("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"); + } + + @Bean + PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); } } diff --git a/samples/rest-notes-slate/src/main/java/com/example/notes/Tag.java b/samples/rest-notes-slate/src/main/java/com/example/notes/Tag.java index 8bd834f9..4ec2570f 100644 --- a/samples/rest-notes-slate/src/main/java/com/example/notes/Tag.java +++ b/samples/rest-notes-slate/src/main/java/com/example/notes/Tag.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,11 @@ package com.example.notes; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/samples/rest-notes-slate/src/main/resources/application.properties b/samples/rest-notes-slate/src/main/resources/application.properties deleted file mode 100644 index 8e06a828..00000000 --- a/samples/rest-notes-slate/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -spring.jackson.serialization.indent_output: true \ No newline at end of file diff --git a/samples/rest-notes-slate/src/test/java/com/example/notes/ApiDocumentation.java b/samples/rest-notes-slate/src/test/java/com/example/notes/ApiDocumentation.java index 0416f6a8..c599ace8 100644 --- a/samples/rest-notes-slate/src/test/java/com/example/notes/ApiDocumentation.java +++ b/samples/rest-notes-slate/src/test/java/com/example/notes/ApiDocumentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,26 +38,28 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import jakarta.servlet.RequestDispatcher; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; import org.springframework.restdocs.JUnitRestDocumentation; import org.springframework.restdocs.payload.JsonFieldType; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.RequestDispatcher; + +@WebAppConfiguration @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = RestNotesSlate.class) public class ApiDocumentation { @Rule diff --git a/samples/rest-notes-spring-data-rest/pom.xml b/samples/rest-notes-spring-data-rest/pom.xml index 67d13cff..bde81c56 100644 --- a/samples/rest-notes-spring-data-rest/pom.xml +++ b/samples/rest-notes-spring-data-rest/pom.xml @@ -8,59 +8,97 @@ 0.0.1-SNAPSHOT jar - - org.springframework.boot - spring-boot-starter-parent - 2.4.7 - - - UTF-8 - 17 - 3.0.0-SNAPSHOT + 17 + 17 + 3.0.0-SNAPSHOT - org.springframework.boot - spring-boot-starter-data-rest + com.fasterxml.jackson.core + jackson-databind + 2.12.5 - org.springframework.boot - spring-boot-starter-data-jpa + jakarta.servlet + jakarta.servlet-api + 5.0.0 + + + org.hibernate.validator + hibernate-validator + 7.0.0.Final + + + org.hibernate + hibernate-core-jakarta + 5.5.7.Final + + + org.springframework + spring-webmvc + 6.0.0-SNAPSHOT + + + org.springframework.data + spring-data-jpa + 3.0.0-SNAPSHOT + + + org.springframework.data + spring-data-rest-webmvc + 4.0.0-SNAPSHOT com.h2database h2 + 1.4.200 + runtime + + + org.atteo + evo-inflector + 1.2.1 runtime - - org.junit.vintage - junit-vintage-engine - test - - - org.hamcrest - hamcrest-core - - - - - org.springframework.boot - spring-boot-starter-test - test - com.jayway.jsonpath json-path + 2.6.0 + test + + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.21.0 + test + + + org.glassfish + jakarta.el + 4.0.2 + test + + + org.hamcrest + hamcrest + 2.2 test org.springframework.restdocs spring-restdocs-mockmvc + ${restdocs.version} test @@ -68,12 +106,8 @@ - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins maven-surefire-plugin + 2.22.2 **/*Documentation.java @@ -104,12 +138,13 @@ org.springframework.restdocs spring-restdocs-asciidoctor - ${spring-restdocs.version} + ${restdocs.version} maven-resources-plugin + 3.2.0 copy-resources diff --git a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/ErrorController.java b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/ErrorController.java new file mode 100644 index 00000000..557090e0 --- /dev/null +++ b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/ErrorController.java @@ -0,0 +1,45 @@ +/* + * Copyright 2014-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.notes; + +import java.time.Instant; +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.http.HttpServletRequest; + +@RestController +class ErrorController { + + @RequestMapping("/error") + ResponseEntity> handleError(HttpServletRequest request) { + HttpStatus httpStatus = HttpStatus.valueOf((int)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)); + Map body = Map.of("status", httpStatus.value(), + "error", httpStatus.getReasonPhrase(), + "path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), + "message", request.getAttribute(RequestDispatcher.ERROR_MESSAGE), + "timestamp", Instant.now().toEpochMilli()); + ResponseEntity> response = new ResponseEntity<>(body, httpStatus); + return response; + } + +} diff --git a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Note.java b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Note.java index 5160682a..e2926477 100644 --- a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Note.java +++ b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Note.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,11 @@ package com.example.notes; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/RestNotesSpringDataRest.java b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/RestNotesSpringDataRest.java index 21195fd5..d9d380e6 100644 --- a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/RestNotesSpringDataRest.java +++ b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/RestNotesSpringDataRest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,58 @@ package com.example.notes; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import javax.sql.DataSource; -@SpringBootApplication +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import jakarta.persistence.EntityManagerFactory; + +@EnableWebMvc +@ComponentScan +@Configuration +@EnableJpaRepositories +@Import(RepositoryRestMvcConfiguration.class) public class RestNotesSpringDataRest { - - public static void main(String[] args) { - SpringApplication.run(RestNotesSpringDataRest.class, args); + + @Bean + ObjectMapper objectMapper() { + return Jackson2ObjectMapperBuilder.json().build(); } - + + @Bean + LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); + factory.setPackagesToScan("com.example.notes"); + HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); + jpaVendorAdapter.setShowSql(true); + jpaVendorAdapter.setGenerateDdl(true); + factory.setJpaVendorAdapter(jpaVendorAdapter); + factory.setDataSource(dataSource); + return factory; + } + + @Bean + DataSource dataSource() { + return new DriverManagerDataSource("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"); + } + + @Bean + PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); + } + } diff --git a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Tag.java b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Tag.java index 8bd834f9..4ec2570f 100644 --- a/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Tag.java +++ b/samples/rest-notes-spring-data-rest/src/main/java/com/example/notes/Tag.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,11 @@ package com.example.notes; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java index 746f3c51..3261af40 100644 --- a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java +++ b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/ApiDocumentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2020 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,19 +44,21 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; import org.springframework.restdocs.JUnitRestDocumentation; import org.springframework.restdocs.payload.JsonFieldType; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.databind.ObjectMapper; +@WebAppConfiguration @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = RestNotesSpringDataRest.class) public class ApiDocumentation { @Rule diff --git a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java index 6dcecb1a..6cdc2f25 100644 --- a/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java +++ b/samples/rest-notes-spring-data-rest/src/test/java/com/example/notes/GettingStartedDocumentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,21 +38,25 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; import org.springframework.restdocs.JUnitRestDocumentation; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; +@Transactional +@WebAppConfiguration @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = RestNotesSpringDataRest.class) public class GettingStartedDocumentation { @Rule diff --git a/samples/rest-notes-spring-hateoas/build.gradle b/samples/rest-notes-spring-hateoas/build.gradle index 52fb3093..e9bc4e19 100644 --- a/samples/rest-notes-spring-hateoas/build.gradle +++ b/samples/rest-notes-spring-hateoas/build.gradle @@ -1,12 +1,8 @@ plugins { - id "eclipse" id "java" id "org.asciidoctor.jvm.convert" version "3.3.2" - id "org.springframework.boot" version "2.4.7" } -apply plugin: 'io.spring.dependency-management' - repositories { mavenLocal() maven { url 'https://repo.spring.io/milestone' } @@ -21,31 +17,34 @@ targetCompatibility = 17 ext { snippetsDir = file('build/generated-snippets') + restdocsVersion = '3.0.0-SNAPSHOT' } -ext['spring-restdocs.version'] = '3.0.0-SNAPSHOT' - configurations { asciidoctorExtensions } dependencies { - asciidoctorExtensions 'org.springframework.restdocs:spring-restdocs-asciidoctor' + asciidoctorExtensions "org.springframework.restdocs:spring-restdocs-asciidoctor:$restdocsVersion" - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation 'org.springframework.boot:spring-boot-starter-hateoas' - implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation "com.fasterxml.jackson.core:jackson-databind:2.12.5" + implementation "jakarta.servlet:jakarta.servlet-api:5.0.0" + implementation "org.hibernate.validator:hibernate-validator:7.0.0.Final" + implementation "org.hibernate:hibernate-core-jakarta:5.5.7.Final" + implementation "org.springframework:spring-webmvc:6.0.0-SNAPSHOT" + implementation "org.springframework.data:spring-data-jpa:3.0.0-SNAPSHOT" + implementation "org.springframework.hateoas:spring-hateoas:2.0.0-SNAPSHOT" - runtimeOnly 'com.h2database:h2' + runtimeOnly 'com.h2database:h2:1.4.200' runtimeOnly 'org.atteo:evo-inflector:1.2.1' - testImplementation 'com.jayway.jsonpath:json-path' - testImplementation 'org.assertj:assertj-core' - testImplementation('org.junit.vintage:junit-vintage-engine') { - exclude group: 'org.hamcrest', module: 'hamcrest-core' - } - testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' + testImplementation 'com.jayway.jsonpath:json-path:2.6.0' + testImplementation 'junit:junit:4.12' + testImplementation 'org.assertj:assertj-core:3.21.0' + testImplementation 'org.hamcrest:hamcrest-library:2.2' + testImplementation "org.springframework.restdocs:spring-restdocs-mockmvc:$restdocsVersion" + + testRuntimeOnly 'org.glassfish:jakarta.el:4.0.2' } test { @@ -58,12 +57,10 @@ asciidoctor { dependsOn test } -bootJar { +jar { dependsOn asciidoctor from ("${asciidoctor.outputDir}/html5") { into 'static/docs' } } -eclipseJdt.onlyIf { false } -cleanEclipseJdt.onlyIf { false } diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ErrorController.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ErrorController.java new file mode 100644 index 00000000..557090e0 --- /dev/null +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ErrorController.java @@ -0,0 +1,45 @@ +/* + * Copyright 2014-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.notes; + +import java.time.Instant; +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.http.HttpServletRequest; + +@RestController +class ErrorController { + + @RequestMapping("/error") + ResponseEntity> handleError(HttpServletRequest request) { + HttpStatus httpStatus = HttpStatus.valueOf((int)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)); + Map body = Map.of("status", httpStatus.value(), + "error", httpStatus.getReasonPhrase(), + "path", request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI), + "message", request.getAttribute(RequestDispatcher.ERROR_MESSAGE), + "timestamp", Instant.now().toEpochMilli()); + ResponseEntity> response = new ResponseEntity<>(body, httpStatus); + return response; + } + +} diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ExceptionSupressingErrorAttributes.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ExceptionSupressingErrorAttributes.java deleted file mode 100644 index 8da9b419..00000000 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/ExceptionSupressingErrorAttributes.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2014-2017 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.notes; - -import java.util.Map; - -import org.springframework.boot.web.error.ErrorAttributeOptions; -import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; -import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestAttributes; -import org.springframework.web.context.request.WebRequest; - -@Component -class ExceptionSupressingErrorAttributes extends DefaultErrorAttributes { - - @Override - public Map getErrorAttributes(WebRequest webRequest, - ErrorAttributeOptions options) { - Map errorAttributes = super.getErrorAttributes(webRequest, options); - errorAttributes.remove("exception"); - Object message = webRequest.getAttribute("jakarta.servlet.error.message", RequestAttributes.SCOPE_REQUEST); - if (message != null) { - errorAttributes.put("message", message); - } - return errorAttributes; - } -} diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Note.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Note.java index 46b349da..d8ffb689 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Note.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Note.java @@ -18,11 +18,11 @@ package com.example.notes; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; @Entity public class Note { diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/RestNotesSpringHateoas.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/RestNotesSpringHateoas.java index f2916a12..48220630 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/RestNotesSpringHateoas.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/RestNotesSpringHateoas.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,58 @@ package com.example.notes; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import javax.sql.DataSource; -@SpringBootApplication +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.hateoas.config.EnableHypermediaSupport; +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import jakarta.persistence.EntityManagerFactory; + +@EnableWebMvc +@ComponentScan +@Configuration +@EnableJpaRepositories +@EnableHypermediaSupport(type = HypermediaType.HAL) class RestNotesSpringHateoas { - public static void main(String[] args) { - SpringApplication.run(RestNotesSpringHateoas.class, args); + @Bean + ObjectMapper objectMapper() { + return Jackson2ObjectMapperBuilder.json().build(); } - + + @Bean + LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { + LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); + factory.setPackagesToScan("com.example.notes"); + HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); + jpaVendorAdapter.setShowSql(true); + jpaVendorAdapter.setGenerateDdl(true); + factory.setJpaVendorAdapter(jpaVendorAdapter); + factory.setDataSource(dataSource); + return factory; + } + + @Bean + DataSource dataSource() { + return new DriverManagerDataSource("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"); + } + + @Bean + PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + return new JpaTransactionManager(entityManagerFactory); + } + } diff --git a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Tag.java b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Tag.java index b8274227..79b11611 100644 --- a/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Tag.java +++ b/samples/rest-notes-spring-hateoas/src/main/java/com/example/notes/Tag.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,11 @@ package com.example.notes; import java.util.List; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToMany; @Entity public class Tag { diff --git a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java index c3e96b51..feaf512b 100644 --- a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java +++ b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/ApiDocumentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2020 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,23 +49,27 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; import org.springframework.restdocs.JUnitRestDocumentation; import org.springframework.restdocs.constraints.ConstraintDescriptions; import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; import org.springframework.restdocs.payload.FieldDescriptor; import org.springframework.restdocs.payload.JsonFieldType; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.databind.ObjectMapper; +@Transactional +@WebAppConfiguration @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = RestNotesSpringHateoas.class) public class ApiDocumentation { @Rule diff --git a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java index 38ab9b60..236c0347 100644 --- a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java +++ b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/GettingStartedDocumentation.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuild import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -41,21 +42,25 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; import org.springframework.restdocs.JUnitRestDocumentation; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.JsonPath; +@Transactional +@WebAppConfiguration @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = RestNotesSpringHateoas.class) public class GettingStartedDocumentation { @Rule @@ -76,6 +81,7 @@ public class GettingStartedDocumentation { .alwaysDo(document("{method-name}/{step}/", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()))) + .alwaysDo(print()) .build(); } diff --git a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/NullOrNotBlankTests.java b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/NullOrNotBlankTests.java index 8b9f1c9a..60bc0e92 100644 --- a/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/NullOrNotBlankTests.java +++ b/samples/rest-notes-spring-hateoas/src/test/java/com/example/notes/NullOrNotBlankTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2018 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import jakarta.validation.Validator; import org.junit.Test; - public class NullOrNotBlankTests { private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); diff --git a/samples/testng/build.gradle b/samples/testng/build.gradle index 5fd0cc4c..091abbbf 100644 --- a/samples/testng/build.gradle +++ b/samples/testng/build.gradle @@ -2,11 +2,8 @@ plugins { id "eclipse" id "java" id "org.asciidoctor.jvm.convert" version "3.3.2" - id "org.springframework.boot" version "2.4.7" } -apply plugin: 'io.spring.dependency-management' - repositories { mavenLocal() maven { url 'https://repo.spring.io/milestone' } @@ -20,21 +17,21 @@ sourceCompatibility = 17 targetCompatibility = 17 ext { + restdocsVersion = '3.0.0-SNAPSHOT' snippetsDir = file('build/generated-snippets') } -ext['spring-restdocs.version'] = '3.0.0-SNAPSHOT' - configurations { asciidoctorExtensions } dependencies { - asciidoctorExtensions 'org.springframework.restdocs:spring-restdocs-asciidoctor' - implementation 'org.springframework.boot:spring-boot-starter-web' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' - testImplementation 'org.testng:testng:6.9.10' + asciidoctorExtensions "org.springframework.restdocs:spring-restdocs-asciidoctor:$restdocsVersion" + + implementation "org.springframework:spring-webmvc:6.0.0-SNAPSHOT" + + testImplementation "org.springframework.restdocs:spring-restdocs-mockmvc:$restdocsVersion" + testImplementation "org.testng:testng:6.9.10" } test { @@ -48,12 +45,9 @@ asciidoctor { dependsOn test } -bootJar { +jar { dependsOn asciidoctor from ("${asciidoctor.outputDir}/html5") { into 'static/docs' } } - -eclipseJdt.onlyIf { false } -cleanEclipseJdt.onlyIf { false } diff --git a/samples/testng/src/main/java/com/example/testng/SampleTestNgApplication.java b/samples/testng/src/main/java/com/example/testng/SampleTestNgApplication.java index f0c6ab91..908a4126 100644 --- a/samples/testng/src/main/java/com/example/testng/SampleTestNgApplication.java +++ b/samples/testng/src/main/java/com/example/testng/SampleTestNgApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,13 @@ package com.example.testng; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -@SpringBootApplication +@Configuration public class SampleTestNgApplication { - public static void main(String[] args) { - new SpringApplication(SampleTestNgApplication.class).run(args); - } - @RestController private static class SampleController { diff --git a/samples/testng/src/test/java/com/example/testng/SampleTestNgApplicationTests.java b/samples/testng/src/test/java/com/example/testng/SampleTestNgApplicationTests.java index 1e0843b7..8606fb26 100644 --- a/samples/testng/src/test/java/com/example/testng/SampleTestNgApplicationTests.java +++ b/samples/testng/src/test/java/com/example/testng/SampleTestNgApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,9 +24,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.lang.reflect.Method; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.restdocs.ManualRestDocumentation; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -34,7 +35,8 @@ import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -@SpringBootTest +@WebAppConfiguration +@ContextConfiguration(classes = SampleTestNgApplication.class) public class SampleTestNgApplicationTests extends AbstractTestNGSpringContextTests { private final ManualRestDocumentation restDocumentation = new ManualRestDocumentation(); diff --git a/samples/web-test-client/build.gradle b/samples/web-test-client/build.gradle index a4dcddd7..2d1f4235 100644 --- a/samples/web-test-client/build.gradle +++ b/samples/web-test-client/build.gradle @@ -18,25 +18,23 @@ sourceCompatibility = 17 targetCompatibility = 17 ext { + restdocsVersion = '3.0.0-SNAPSHOT' snippetsDir = file('build/generated-snippets') } -ext['spring-restdocs.version'] = '3.0.0-SNAPSHOT' - configurations { asciidoctorExtensions } dependencies { - asciidoctorExtensions "org.springframework.restdocs:spring-restdocs-asciidoctor:${project.ext['spring-restdocs.version']}" + asciidoctorExtensions "org.springframework.restdocs:spring-restdocs-asciidoctor:$restdocsVersion" - implementation 'io.projectreactor.ipc:reactor-netty:0.7.1.RELEASE' - implementation 'org.springframework:spring-context:5.0.1.RELEASE' - implementation 'org.springframework:spring-webflux:5.0.1.RELEASE' + implementation 'org.springframework:spring-context:6.0.0-SNAPSHOT' + implementation 'org.springframework:spring-webflux:6.0.0-SNAPSHOT' testImplementation 'junit:junit:4.12' - testImplementation 'org.springframework:spring-test:5.0.1.RELEASE' - testImplementation "org.springframework.restdocs:spring-restdocs-webtestclient:${project.ext['spring-restdocs.version']}" + testImplementation 'org.springframework:spring-test:6.0.0-SNAPSHOT' + testImplementation "org.springframework.restdocs:spring-restdocs-webtestclient:$restdocsVersion" } test { @@ -55,6 +53,3 @@ jar { into 'static/docs' } } - -eclipseJdt.onlyIf { false } -cleanEclipseJdt.onlyIf { false } diff --git a/samples/web-test-client/src/main/java/com/example/webtestclient/SampleWebTestClientApplication.java b/samples/web-test-client/src/main/java/com/example/webtestclient/SampleWebTestClientApplication.java index 7c4d0bd0..4ae6bc48 100644 --- a/samples/web-test-client/src/main/java/com/example/webtestclient/SampleWebTestClientApplication.java +++ b/samples/web-test-client/src/main/java/com/example/webtestclient/SampleWebTestClientApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2017 the original author or authors. + * Copyright 2014-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,33 +16,22 @@ package com.example.webtestclient; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; -import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; -import reactor.ipc.netty.http.server.HttpServer; - @EnableWebFlux @Configuration public class SampleWebTestClientApplication { @Bean public RouterFunction routerFunction() { - return RouterFunctions.route(RequestPredicates.GET("/"), (request) -> ServerResponse.status(HttpStatus.OK).syncBody("Hello, World")); - } - - public static void main(String[] args) { - RouterFunction routerFunction = new AnnotationConfigApplicationContext(SampleWebTestClientApplication.class).getBean(RouterFunction.class); - ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(RouterFunctions.toHttpHandler(routerFunction)); - HttpServer httpServer = HttpServer.create(8080); - httpServer.startAndAwait(adapter); + return RouterFunctions.route(RequestPredicates.GET("/"), (request) -> ServerResponse.status(HttpStatus.OK).bodyValue("Hello, World")); } }