Reformatted code
This commit is contained in:
+3
-3
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-6
@@ -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) {
|
||||
}
|
||||
|
||||
+12
-10
@@ -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) {
|
||||
}
|
||||
|
||||
+3
-3
@@ -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);
|
||||
|
||||
+3
-4
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user