mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-02-11 00:51:12 +00:00
Enhance Owner List, Add Validation & Swagger UI
This commit is contained in:
parent
a4fcf04c93
commit
6b52068e80
5 changed files with 47 additions and 1 deletions
5
pom.xml
5
pom.xml
|
|
@ -114,6 +114,11 @@
|
|||
<version>${webjars-font-awesome.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.6.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
|||
|
|
@ -29,10 +29,12 @@ public class Person extends BaseEntity {
|
|||
|
||||
@Column
|
||||
@NotBlank
|
||||
@jakarta.validation.constraints.Pattern(regexp = "^[a-zA-Z\\s'-]+$", message = "must contain only letters, spaces, hyphens, or apostrophes")
|
||||
private String firstName;
|
||||
|
||||
@Column
|
||||
@NotBlank
|
||||
@jakarta.validation.constraints.Pattern(regexp = "^[a-zA-Z\\s'-]+$", message = "must contain only letters, spaces, hyphens, or apostrophes")
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,8 @@ import org.springframework.validation.Validator;
|
|||
/**
|
||||
* <code>Validator</code> for <code>Pet</code> forms.
|
||||
* <p>
|
||||
* We're not using Bean Validation annotations here because it is easier to define such
|
||||
* We're not using Bean Validation annotations here because it is easier to
|
||||
* define such
|
||||
* validation rule in Java.
|
||||
* </p>
|
||||
*
|
||||
|
|
@ -38,9 +39,23 @@ public class PetValidator implements Validator {
|
|||
Pet pet = (Pet) obj;
|
||||
String name = pet.getName();
|
||||
// name validation
|
||||
|
||||
if (!StringUtils.hasText(name)) {
|
||||
errors.rejectValue("name", REQUIRED, REQUIRED);
|
||||
}
|
||||
// Edge Case: Check for leading/trailing spaces if trimmed but not null
|
||||
else if (!name.equals(name.trim())) {
|
||||
errors.rejectValue("name", "leadingTrailingSpace", "cannot start or end with spaces");
|
||||
}
|
||||
// Edge Case: Max length
|
||||
else if (name.length() > 30) {
|
||||
errors.rejectValue("name", "maxLength", "must be 30 characters or less");
|
||||
}
|
||||
// Edge Case: Symbols/Special Chars
|
||||
else if (!name.matches("^[a-zA-Z0-9\\s'-]+$")) {
|
||||
errors.rejectValue("name", "invalidCharacters",
|
||||
"contains invalid characters (only letters, numbers, spaces, hyphens allowed)");
|
||||
}
|
||||
|
||||
// type validation
|
||||
if (pet.isNew() && pet.getType() == null) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package org.springframework.samples.petclinic.system;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.info.License;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class OpenApiConfiguration {
|
||||
|
||||
@Bean
|
||||
public OpenAPI petClinicOpenAPI() {
|
||||
return new OpenAPI().info(new Info().title("Spring PetClinic API")
|
||||
.description("Spring PetClinic sample application")
|
||||
.version("v0.0.1")
|
||||
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -9,15 +9,18 @@
|
|||
<table id="owners" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 50px;">ID</th>
|
||||
<th th:text="#{name}" style="width: 150px;">Name</th>
|
||||
<th th:text="#{address}" style="width: 200px;">Address</th>
|
||||
<th th:text="#{city}">City</th>
|
||||
<th th:text="#{telephone}" style="width: 120px">Telephone</th>
|
||||
<th th:text="#{pets}">Pets</th>
|
||||
<th style="width: 100px;">Pet Count</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="owner : ${listOwners}">
|
||||
<td th:text="${owner.id}" />
|
||||
<td>
|
||||
<a th:href="@{/owners/__${owner.id}__}" th:text="${owner.firstName + ' ' + owner.lastName}" /></a>
|
||||
</td>
|
||||
|
|
@ -25,6 +28,7 @@
|
|||
<td th:text="${owner.city}" />
|
||||
<td th:text="${owner.telephone}" />
|
||||
<td><span th:text="${#strings.listJoin(owner.pets, ', ')}" /></td>
|
||||
<td th:text="${owner.pets.size()}" />
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue