Reformatted code

This commit is contained in:
Graham Cox
2022-05-19 10:14:05 +01:00
parent 177f912393
commit 762d5f0f56
27 changed files with 278 additions and 283 deletions
+40 -40
View File
@@ -1,45 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>api-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>api-service</name>
<description>Aggregator Service for LightRun Article</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>api-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>api-service</name>
<description>Aggregator Service for LightRun Article</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApiServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ApiServiceApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(ApiServiceApplication.class, args);
}
}
@@ -1,20 +1,20 @@
package com.baeldung.apiservice;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class RequestIdGenerator implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestId = UUID.randomUUID().toString();
String requestId = UUID.randomUUID()
.toString();
MDC.put(RequestIdGenerator.class.getCanonicalName(), requestId);
response.addHeader("X-Request-Id", requestId);
@@ -9,12 +9,12 @@ import org.springframework.web.client.RestTemplate;
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.additionalInterceptors((request, body, execution) -> {
request.getHeaders().add("X-Request-Id", RequestIdGenerator.getRequestId());
return builder.additionalInterceptors((request, body, execution) -> {
request.getHeaders()
.add("X-Request-Id", RequestIdGenerator.getRequestId());
return execution.execute(request, body);
})
.build();
return execution.execute(request, body);
})
.build();
}
}
@@ -2,10 +2,5 @@ package com.baeldung.apiservice.adapters.http;
import java.time.Instant;
public record TaskResponse(String id,
String title,
Instant created,
UserResponse createdBy,
UserResponse assignedTo,
String status) {
public record TaskResponse(String id, String title, Instant created, UserResponse createdBy, UserResponse assignedTo, String status) {
}
@@ -1,11 +1,17 @@
package com.baeldung.apiservice.adapters.http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.apiservice.adapters.tasks.Task;
import com.baeldung.apiservice.adapters.tasks.TaskRepository;
import com.baeldung.apiservice.adapters.users.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/")
@RestController
@@ -27,15 +33,10 @@ public class TasksController {
}
private TaskResponse buildResponse(Task task) {
return new TaskResponse(task.id(),
task.title(),
task.created(),
getUser(task.createdBy()),
getUser(task.assignedTo()),
task.status());
return new TaskResponse(task.id(), task.title(), task.created(), getUser(task.createdBy()), getUser(task.assignedTo()), task.status());
}
private UserResponse getUser(String userId) {
private UserResponse getUser(String userId) {
if (userId == null) {
return null;
}
@@ -47,6 +48,7 @@ public class TasksController {
return new UserResponse(user.id(), user.name());
}
@ExceptionHandler(UnknownTaskException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleUnknownTask() {
@@ -2,10 +2,5 @@ package com.baeldung.apiservice.adapters.tasks;
import java.time.Instant;
public record Task(String id,
String title,
Instant created,
String createdBy,
String assignedTo,
String status) {
public record Task(String id, String title, Instant created, String createdBy, String assignedTo, String status) {
}
@@ -17,9 +17,9 @@ public class TaskRepository {
public Task getTaskById(String id) {
var uri = UriComponentsBuilder.fromUriString(tasksService)
.path(id)
.build()
.toUri();
.path(id)
.build()
.toUri();
try {
return restTemplate.getForObject(uri, Task.class);
@@ -1,6 +1,5 @@
package com.baeldung.apiservice.adapters.users;
import com.baeldung.apiservice.adapters.users.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@@ -18,9 +17,9 @@ public class UserRepository {
public User getUserById(String id) {
var uri = UriComponentsBuilder.fromUriString(usersService)
.path(id)
.build()
.toUri();
.path(id)
.build()
.toUri();
try {
return restTemplate.getForObject(uri, User.class);