Skip to content

Commit df416e1

Browse files
Merge pull request #1 from hndrs/add-exception-handler
Adds ExceptionHandler and ResponseAdvise Autoconfiguration
2 parents 23f6f65 + f5855f3 commit df416e1

File tree

17 files changed

+293
-21
lines changed

17 files changed

+293
-21
lines changed

README.md

Whitespace-only changes.

build.gradle.kts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
object Versions {
4-
const val KOTLIN_VERSION = "1.4.10"
4+
const val KOTLIN_VERSION = "1.4.30"
55
}
66

77
buildscript {
@@ -18,9 +18,9 @@ plugins {
1818
id("org.sonarqube").version("3.0")
1919
`maven-publish`
2020
id("io.spring.dependency-management") version "1.0.10.RELEASE"
21-
kotlin("jvm").version("1.4.0")
22-
kotlin("plugin.spring").version("1.4.0")
23-
kotlin("kapt").version("1.4.0")
21+
kotlin("jvm").version("1.4.30")
22+
kotlin("plugin.spring").version("1.4.30")
23+
kotlin("kapt").version("1.4.30")
2424
id("java")
2525
id("maven-publish")
2626
id("idea")
@@ -54,7 +54,7 @@ subprojects {
5454
cacheChangingModulesFor(0, "seconds")
5555
}
5656
imports {
57-
mavenBom("org.springframework.boot:spring-boot-dependencies:2.4.2.RELEASE") {
57+
mavenBom("org.springframework.boot:spring-boot-dependencies:2.4.2") {
5858
bomProperty("kotlin.version", Versions.KOTLIN_VERSION)
5959
}
6060
}
@@ -76,7 +76,7 @@ subprojects {
7676
tasks.withType<KotlinCompile> {
7777
kotlinOptions {
7878
freeCompilerArgs = listOf("-Xjsr305=strict")
79-
jvmTarget = "11"
79+
jvmTarget = "15"
8080
}
8181
}
8282

@@ -86,25 +86,28 @@ subprojects {
8686
from(sourceSets["main"].allSource)
8787
}
8888

89-
publishing {
90-
repositories {
91-
92-
}
93-
publications {
94-
create<MavenPublication>(project.name) {
95-
from(components["java"])
96-
artifact(sourcesJarSubProject)
97-
98-
groupId = rootProject.group as? String
99-
artifactId = project.name
100-
version = "${rootProject.version}${project.findProperty("version.appendix") ?: ""}"
89+
if (project.name != "sample") {
90+
println(project.name)
91+
publishing {
92+
repositories {
93+
94+
}
95+
publications {
96+
create<MavenPublication>(project.name) {
97+
from(components["java"])
98+
artifact(sourcesJarSubProject)
99+
100+
groupId = rootProject.group as? String
101+
artifactId = project.name
102+
version = "${rootProject.version}${project.findProperty("version.appendix") ?: ""}"
103+
}
101104
}
102-
}
103105

106+
}
104107
}
105108

106109
configure<JacocoPluginExtension> {
107-
toolVersion = "0.8.5"
110+
toolVersion = "0.8.6"
108111
}
109112

110113
tasks.withType<JacocoReport> {

sample/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id("org.springframework.boot").version("2.4.2")
3+
}
4+
repositories {
5+
mavenCentral()
6+
}
7+
8+
dependencies {
9+
implementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
10+
implementation(project(":spring-json-api-starter"))
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.hndrs.api.sample
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
6+
@SpringBootApplication
7+
open class SampleApplication {
8+
9+
}
10+
11+
fun main(args: Array<String>) {
12+
runApplication<SampleApplication>(*args)
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.hndrs.api.sample
2+
3+
import io.hndrs.api.response.JsonApiResponse
4+
import io.hndrs.api.response.Response
5+
import org.springframework.http.HttpStatus
6+
import org.springframework.http.ResponseEntity
7+
import org.springframework.web.bind.annotation.GetMapping
8+
import org.springframework.web.bind.annotation.RestController
9+
import org.springframework.web.server.ResponseStatusException
10+
11+
@RestController
12+
class SampleController {
13+
14+
15+
@GetMapping("/test-errors")
16+
fun errors() {
17+
throw ResponseStatusException(HttpStatus.CONFLICT)
18+
}
19+
20+
@GetMapping("/address1")
21+
@JsonApiResponse
22+
fun annotated(): Address {
23+
return Address()
24+
}
25+
26+
@GetMapping("/address2")
27+
fun wrapped(): Response<Address> {
28+
return Response(Address())
29+
}
30+
31+
@GetMapping("/address3")
32+
@JsonApiResponse
33+
fun annotatedResponseEntity(): ResponseEntity<Address> {
34+
return ResponseEntity.ok(Address())
35+
}
36+
37+
data class Address(
38+
val street: String = "147 Bleecker St",
39+
val locality: String = "New York",
40+
val zip: String = "10012"
41+
)
42+
}

settings.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ project(":spring-json-api").projectDir = File("spring-json-api")
55

66
include("spring-json-api-starter")
77
project(":spring-json-api-starter").projectDir = File("spring-json-api-starter")
8+
9+
include("sample")
10+
project(":sample").projectDir = File("sample")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
dependencies {
2+
api(project(":spring-json-api"))
3+
testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
24
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.hndrs.api.autoconfigure
2+
3+
import io.hndrs.api.exception.ExceptionHandler
4+
import io.hndrs.api.response.ResponseAdvice
5+
import org.springframework.context.annotation.Bean
6+
import org.springframework.context.annotation.Configuration
7+
8+
@Configuration
9+
open class JsonApiAutoConfiguration {
10+
11+
@Bean
12+
open fun exceptionAdvice(): ExceptionHandler {
13+
return ExceptionHandler()
14+
}
15+
16+
@Bean
17+
open fun responseAdvice(): ResponseAdvice {
18+
return ResponseAdvice()
19+
}
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
io.hndrs.api.autoconfigure.JsonApiAutoConfiguration
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.hndrs.api.autoconfigure
2+
3+
import io.hndrs.api.exception.ExceptionHandler
4+
import io.hndrs.api.response.ResponseAdvice
5+
import org.junit.jupiter.api.Assertions
6+
import org.junit.jupiter.api.DisplayName
7+
import org.junit.jupiter.api.Test
8+
import org.springframework.boot.autoconfigure.AutoConfigurations
9+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner
10+
11+
@DisplayName("JsonApi Autoconfiguration")
12+
internal class JsonApiAutoConfigurationTest {
13+
14+
@Test
15+
fun test() {
16+
WebApplicationContextRunner()
17+
.withConfiguration(
18+
AutoConfigurations.of(JsonApiAutoConfiguration::class.java)
19+
).run {
20+
Assertions.assertNotNull(it.getBean(ExceptionHandler::class.java))
21+
Assertions.assertNotNull(it.getBean(ResponseAdvice::class.java))
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)