JAVA-29170: Changes made for Upgrade spring-rest-http to Spring Boot 3 (#15823)

* JAVA-29170: Changes made for Upgrade spring-rest-http to Spring Boot 3

* JAVA-29170: Changes made for Upgrade spring-rest-http to Spring Boot 3

* JAVA-29170: Changes made for Upgrade spring-rest-http to Spring Boot 3

* JAVA-29170: Changes made for Upgrade spring-rest-http to Spring Boot 3
This commit is contained in:
Bipin kumar 2024-02-13 22:49:41 +05:30 committed by GitHub
parent 557d43c300
commit d3911cd084
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 27 deletions

View File

@ -10,9 +10,9 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
@ -54,11 +54,30 @@
<artifactId>json-patch</artifactId>
<version>${jsonpatch.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<xstream.version>1.4.9</xstream.version>
<jsonpatch.version>1.12</jsonpatch.version>
<httpclient5.version>5.2.1</httpclient5.version>
</properties>
</project>

View File

@ -1,22 +1,22 @@
package com.baeldung.responseheaders.controllers;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/filter-response-header")
public class FilterResponseHeaderController {
@GetMapping("/no-extra-header")
public String FilterHeaderResponseWithNoExtraHeader() {
public String filterHeaderResponseWithNoExtraHeader() {
return "Response body with Filter header and no extra header";
}
@GetMapping("/extra-header")
public String FilterHeaderResponseWithExtraHeader(HttpServletResponse response) {
public String filterHeaderResponseWithExtraHeader(HttpServletResponse response) {
response.addHeader("Baeldung-Example-Header", "Value-ExtraHeader");
return "Response body with Filter header and one extra header";
}

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

View File

@ -2,18 +2,18 @@ package com.baeldung.responseheaders.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletResponse;
@WebFilter("/filter-response-header/*")
public class AddResponseHeaderFilter implements Filter {

View File

@ -23,8 +23,6 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/customers")
public class CustomerRestController {
@ -40,25 +38,28 @@ public class CustomerRestController {
public ResponseEntity<Customer> createCustomer(@RequestBody Customer customer) {
Customer customerCreated = customerService.createCustomer(customer);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(customerCreated.getId())
.toUri();
return ResponseEntity.created(location).build();
.path("/{id}")
.buildAndExpand(customerCreated.getId())
.toUri();
return ResponseEntity.created(location)
.build();
}
@PatchMapping(path = "/{id}", consumes = "application/json-patch+json")
public ResponseEntity<Customer> updateCustomer(@PathVariable String id,
@RequestBody JsonPatch patch) {
public ResponseEntity<Customer> updateCustomer(@PathVariable String id, @RequestBody JsonPatch patch) {
try {
Customer customer = customerService.findCustomer(id).orElseThrow(CustomerNotFoundException::new);
Customer customer = customerService.findCustomer(id)
.orElseThrow(CustomerNotFoundException::new);
Customer customerPatched = applyPatchToCustomer(patch, customer);
customerService.updateCustomer(customerPatched);
return ResponseEntity.ok(customerPatched);
} catch (CustomerNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.build();
} catch (JsonPatchException | JsonProcessingException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build();
}
}