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

View File

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

View File

@ -2,18 +2,18 @@ package com.baeldung.responseheaders.filter;
import java.io.IOException; 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.Logger;
import org.slf4j.LoggerFactory; 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/*") @WebFilter("/filter-response-header/*")
public class AddResponseHeaderFilter implements Filter { public class AddResponseHeaderFilter implements Filter {

View File

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