Skip to content

Commit 5121a1e

Browse files
committed
chore: clean codes
1 parent b09e33c commit 5121a1e

File tree

12 files changed

+52
-63
lines changed

12 files changed

+52
-63
lines changed

pom.xml

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,43 @@
2121
<properties>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24-
<java.version>17</java.version>
24+
<java.version>21</java.version>
2525
<jjwt.version>0.12.6</jjwt.version>
2626
</properties>
2727

2828
<dependencies>
2929
<dependency>
3030
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
<artifactId>spring-boot-starter-web</artifactId>
3232
</dependency>
3333
<dependency>
3434
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-security</artifactId>
35+
<artifactId>spring-boot-starter-hateoas</artifactId>
3636
</dependency>
3737
<dependency>
3838
<groupId>org.springframework.boot</groupId>
39-
<artifactId>spring-boot-starter-web</artifactId>
39+
<artifactId>spring-boot-starter-validation</artifactId>
4040
</dependency>
41-
4241
<dependency>
4342
<groupId>org.springframework.boot</groupId>
44-
<artifactId>spring-boot-starter-validation</artifactId>
43+
<artifactId>spring-boot-starter-security</artifactId>
4544
</dependency>
46-
4745
<dependency>
4846
<groupId>org.springframework.boot</groupId>
49-
<artifactId>spring-boot-starter-hateoas</artifactId>
47+
<artifactId>spring-boot-starter-data-jpa</artifactId>
5048
</dependency>
5149

5250
<dependency>
5351
<groupId>org.springframework.boot</groupId>
5452
<artifactId>spring-boot-starter-data-rest</artifactId>
5553
</dependency>
5654

55+
<dependency>
56+
<groupId>org.postgresql</groupId>
57+
<artifactId>postgresql</artifactId>
58+
<scope>runtime</scope>
59+
</dependency>
60+
5761
<dependency>
5862
<groupId>org.projectlombok</groupId>
5963
<artifactId>lombok</artifactId>
@@ -79,11 +83,6 @@
7983
<scope>runtime</scope>
8084
</dependency>
8185

82-
<dependency>
83-
<groupId>org.postgresql</groupId>
84-
<artifactId>postgresql</artifactId>
85-
</dependency>
86-
8786
<dependency>
8887
<groupId>org.springframework.boot</groupId>
8988
<artifactId>spring-boot-starter-test</artifactId>
@@ -113,9 +112,29 @@
113112

114113
<build>
115114
<plugins>
115+
<plugin>
116+
<groupId>org.apache.maven.plugins</groupId>
117+
<artifactId>maven-compiler-plugin</artifactId>
118+
<configuration>
119+
<annotationProcessorPaths>
120+
<path>
121+
<groupId>org.projectlombok</groupId>
122+
<artifactId>lombok</artifactId>
123+
</path>
124+
</annotationProcessorPaths>
125+
</configuration>
126+
</plugin>
116127
<plugin>
117128
<groupId>org.springframework.boot</groupId>
118129
<artifactId>spring-boot-maven-plugin</artifactId>
130+
<configuration>
131+
<excludes>
132+
<exclude>
133+
<groupId>org.projectlombok</groupId>
134+
<artifactId>lombok</artifactId>
135+
</exclude>
136+
</excludes>
137+
</configuration>
119138
</plugin>
120139
</plugins>
121140
</build>

src/main/java/com/example/demo/config/AppConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
66
import org.springframework.security.crypto.password.PasswordEncoder;
77

8-
@Configuration
8+
@Configuration(proxyBeanMethods = false)
99
public class AppConfig {
1010

1111
@Bean

src/main/java/com/example/demo/domain/Username.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
import jakarta.persistence.Embeddable;
88

9-
@Data
109
@Embeddable
11-
@AllArgsConstructor
12-
@NoArgsConstructor
13-
public class Username {
14-
private String username;
10+
public record Username(String username) {
1511
}

src/main/java/com/example/demo/web/AuthenticationController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class AuthenticationController {
3434
public ResponseEntity signin(@RequestBody AuthenticationRequest data) {
3535

3636
try {
37-
String username = data.getUsername();
38-
var authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, data.getPassword()));
37+
String username = data.username();
38+
var authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, data.password()));
3939
String token = jwtTokenProvider.createToken(authentication);
4040
Map<Object, Object> model = new HashMap<>();
4141
model.put("username", username);
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
package com.example.demo.web;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Builder;
5-
import lombok.Data;
6-
import lombok.NoArgsConstructor;
3+
public record AuthenticationRequest(String username, String password) {
74

8-
import java.io.Serializable;
9-
10-
@Data
11-
@Builder
12-
@NoArgsConstructor
13-
@AllArgsConstructor
14-
public class AuthenticationRequest implements Serializable {
15-
private static final long serialVersionUID = -6986746375915710855L;
16-
private String username;
17-
private String password;
185
}

src/main/java/com/example/demo/web/VehicleController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ResponseEntity<List<Vehicle>> all(@RequestParam(name = "brand", required
3838
@SuppressWarnings("rawtypes")
3939
@PostMapping("")
4040
public ResponseEntity save(@RequestBody VehicleForm form, HttpServletRequest request) {
41-
Vehicle saved = this.vehicles.save(Vehicle.builder().name(form.getName()).build());
41+
Vehicle saved = this.vehicles.save(Vehicle.builder().name(form.name()).build());
4242
return created(
4343
ServletUriComponentsBuilder
4444
.fromContextPath(request)
@@ -50,14 +50,14 @@ public ResponseEntity save(@RequestBody VehicleForm form, HttpServletRequest req
5050

5151
@GetMapping("/{id}")
5252
public ResponseEntity<Vehicle> get(@PathVariable("id") Long id) {
53-
return ok(this.vehicles.findById(id).orElseThrow(() -> new VehicleNotFoundException()));
53+
return ok(this.vehicles.findById(id).orElseThrow(() -> new VehicleNotFoundException(id)));
5454
}
5555

5656
@SuppressWarnings("rawtypes")
5757
@PutMapping("/{id}")
5858
public ResponseEntity update(@PathVariable("id") Long id, @RequestBody VehicleForm form) {
59-
Vehicle existed = this.vehicles.findById(id).orElseThrow(() -> new VehicleNotFoundException());
60-
existed.setName(form.getName());
59+
Vehicle existed = this.vehicles.findById(id).orElseThrow(() -> new VehicleNotFoundException(id));
60+
existed.setName(form.name());
6161

6262
this.vehicles.save(existed);
6363
return noContent().build();
@@ -66,7 +66,7 @@ public ResponseEntity update(@PathVariable("id") Long id, @RequestBody VehicleFo
6666
@SuppressWarnings("rawtypes")
6767
@DeleteMapping("/{id}")
6868
public ResponseEntity delete(@PathVariable("id") Long id) {
69-
Vehicle existed = this.vehicles.findById(id).orElseThrow(() -> new VehicleNotFoundException());
69+
Vehicle existed = this.vehicles.findById(id).orElseThrow(() -> new VehicleNotFoundException(id));
7070
this.vehicles.delete(existed);
7171
return noContent().build();
7272
}
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
package com.example.demo.web;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Builder;
5-
import lombok.Data;
6-
import lombok.NoArgsConstructor;
7-
8-
@Data
9-
@Builder
10-
@NoArgsConstructor
11-
@AllArgsConstructor
12-
public class VehicleForm {
13-
private String name;
3+
public record VehicleForm(String name) {
144
}

src/main/java/com/example/demo/web/VehicleNotFoundException.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.example.demo.web;
22

33
public class VehicleNotFoundException extends RuntimeException {
4-
public VehicleNotFoundException() {
5-
}
6-
74
public VehicleNotFoundException(Long vehicleId ) {
85
super("Vehicle: " +vehicleId +" not found.");
96
}

src/test/java/com/example/demo/ApplicationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testSave() throws Exception {
5454
this.mockMvc
5555
.perform(
5656
post("/v1/vehicles")
57-
.content(this.objectMapper.writeValueAsBytes(VehicleForm.builder().name("test").build()))
57+
.content(this.objectMapper.writeValueAsBytes(new VehicleForm("test")))
5858
.contentType(MediaType.APPLICATION_JSON)
5959
)
6060
.andExpect(status().is4xxClientError());
@@ -68,7 +68,7 @@ public void testSaveWithMock() throws Exception {
6868
this.mockMvc
6969
.perform(
7070
post("/v1/vehicles")
71-
.content(this.objectMapper.writeValueAsBytes(VehicleForm.builder().name("test").build()))
71+
.content(this.objectMapper.writeValueAsBytes(new VehicleForm("test")))
7272
.contentType(MediaType.APPLICATION_JSON)
7373
)
7474
.andExpect(status().isCreated());

src/test/java/com/example/demo/IntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void setup() {
3535
RestAssured.port = this.port;
3636
token = given()
3737
.contentType(ContentType.JSON)
38-
.body(AuthenticationRequest.builder().username("user").password("password").build())
38+
.body(new AuthenticationRequest("user","password"))
3939
.when().post("/auth/signin")
4040
.andReturn().jsonPath().getString("token");
4141
log.debug("Got token:" + token);
@@ -63,7 +63,7 @@ public void testSave() throws Exception {
6363
given()
6464

6565
.contentType(ContentType.JSON)
66-
.body(VehicleForm.builder().name("test").build())
66+
.body(new VehicleForm("test"))
6767

6868
.when()
6969
.post("/v1/vehicles")
@@ -81,7 +81,7 @@ public void testSaveWithAuth() throws Exception {
8181
given()
8282
.header("Authorization", "Bearer "+token)
8383
.contentType(ContentType.JSON)
84-
.body(VehicleForm.builder().name("test").build())
84+
.body(new VehicleForm("test"))
8585

8686
.when()
8787
.post("/v1/vehicles")
@@ -98,7 +98,7 @@ public void testSaveWithInvalidAuth() throws Exception {
9898
given()
9999
.header("Authorization", "Bearer "+"invalidtoken")
100100
.contentType(ContentType.JSON)
101-
.body(VehicleForm.builder().name("test").build())
101+
.body(new VehicleForm("test"))
102102

103103
.when()
104104
.post("/v1/vehicles")

0 commit comments

Comments
 (0)