Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spring-web-lab/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pl.mperor.lab.spring.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/hello")
@RestController
Expand All @@ -13,4 +13,19 @@ String hello() {
return "🗺️ Hello World!";
}

}
@PostMapping("/{name}")
String welcomeName(@PathVariable @NotBlank String name) {
return "🤚 Welcome %s!".formatted(name);
}

@PostMapping("/user")
String welcomeUser(@RequestBody @Valid HelloUser user) {
return "🤚 Welcome 👤 %s (%s)!".formatted(user.name, user.email);
}

record HelloUser(@NotNull @Size(min = 3) String name,
@NotNull @Email String email,
@Min(0) Integer age,
@Pattern(regexp = "^\\d{3}[\\s-]?\\d{3}[\\s-]?\\d{3}$") String phoneNumber) {
}
}
6 changes: 5 additions & 1 deletion spring-web-lab/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
springdoc:
swagger-ui:
use-root-path: true
display-request-duration: true
display-request-duration: true

server:
error:
include-binding-errors: always
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.assertj.MockMvcTester;

@WebMvcTest
Expand All @@ -19,4 +20,27 @@ void shouldReturnHelloWorld() {
.assertThat().hasStatusOk()
.bodyText().isEqualTo("🗺️ Hello World!");
}

@Test
void shouldReturnWelcomeWhenNameNotBlank() {
tester.post()
.uri("/hello/{name}", "NotBlank")
.exchange()
.assertThat().hasStatusOk()
.bodyText().isEqualTo("🤚 Welcome NotBlank!");
}

@Test
void shouldReturn400WhenNotValidContent() {
tester.post()
.uri("/hello/user")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"name": "josh",
"email": "not a valid email"
}""")
.exchange()
.assertThat().hasStatus4xxClientError();
}
}