Post

2023-04-19-TIL

2023-04-19-TIL

Today I Learned

Can I request multiple content type in a single HTTP request?

불가능하다.

How to Request JSON in Multipart Request

  • https://community.boomi.com/s/article/json-to-multipart
  • https://stackoverflow.com/questions/21329426/spring-mvc-multipart-request-with-json
  • https://blogs.perficient.com/2020/07/27/requestbody-and-multipart-on-spring-boot/
  • https://swagger.io/docs/specification/describing-request-body/multipart-requests/
  • https://community.postman.com/t/postman-with-a-file-and-json-object-in-same-request-as-multipart/6906
  • https://www.baeldung.com/sprint-boot-multipart-requests

Mapstruct Usage

  • https://recordsoflife.tistory.com/781
  • https://velog.io/@cham/Java-MapStruct-%EC%82%AC%EC%9A%A9%EB%B2%95
  • https://github.com/spring-petclinic/spring-petclinic-rest
  • https://mapstruct.org/documentation/stable/api/
  • https://www.baeldung.com/mapstruct-multiple-source-objects
  • https://stackoverflow.com/questions/47676369/mapstruct-and-lombok-not-working-together
  • https://stackoverflow.com/questions/61322386/mapper-not-mapping-properly-mapstruct

Spring @Transactional and Exceptions

  • https://medium.com/codex/spring-transactional-and-exceptions-46ef031c75fb

@Transactional(readOnly = true)

  • https://cupeanimus.tistory.com/90
  • https://www.baeldung.com/spring-transactions-read-only
  • https://perfectacle.github.io/2021/08/08/readonly-transaction-doesnt-make-entity-snapshot/

WebClient vs RestTemplate

  • https://tecoble.techcourse.co.kr/post/2021-07-25-resttemplate-webclient/
  • https://www.baeldung.com/spring-webclient-resttemplate
  • https://recordsoflife.tistory.com/1311

Q. Is it okay to use WebClient instead of RestTemplate?

A. Yes, it is perfectly fine to use WebClient instead of RestTemplate in Java.

WebClient is a reactive HTTP client that provides a non-blocking and efficient way to make HTTP requests. It supports both synchronous and asynchronous programming models and provides a more functional approach to building and executing requests.

RestTemplate, on the other hand, is a traditional synchronous HTTP client that has been around for a long time. It is still widely used and supported, but it has some limitations, such as the inability to handle reactive streams and being less flexible than WebClient.

If you are working with Spring Boot 2.x and above, WebClient is the recommended HTTP client to use. However, if you are working with an older version of Spring Boot or have existing code that uses RestTemplate, you can continue to use it. Just keep in mind that RestTemplate may not be as efficient or flexible as WebClient.

Q. WebClient vs RestTemplate

A. WebClient and RestTemplate are both Java libraries used for making HTTP requests. However, there are some differences between the two that are worth noting:

  1. Synchronous vs Asynchronous: RestTemplate is a synchronous library, meaning that it blocks the thread until a response is received. WebClient, on the other hand, is an asynchronous library that allows for non-blocking I/O and can handle a large number of concurrent requests.
  2. Reactive Programming: WebClient is part of the Spring WebFlux framework, which supports reactive programming. This means that it can handle streams of data and has built-in support for reactive types such as Mono and Flux. RestTemplate, on the other hand, does not support reactive programming.
  3. Configuration: RestTemplate has a simple configuration model that allows for easy customization of the HTTP request and response. WebClient, on the other hand, has a more complex configuration model, but it provides more flexibility and fine-grained control over the request and response.
  4. Performance: Because WebClient is asynchronous, it can handle a large number of requests more efficiently than RestTemplate, which is synchronous.

In summary, both WebClient and RestTemplate are good libraries for making HTTP requests in Java, but they have different use cases. RestTemplate is a good choice for simple, synchronous requests, while WebClient is better suited for reactive programming and high-performance, concurrent requests.

Customize the Jackson ObjectMapper

  • https://www.baeldung.com/spring-boot-customize-jackson-objectmapper
  • https://addio3305.tistory.com/101
  • https://stackoverflow.com/questions/48519772/how-to-configure-jackson-in-spring-boot-application-without-overriding-springs-d
  • https://memo-the-day.tistory.com/208
  • https://codingnconcepts.com/spring-boot/customize-jackson-json-mapper/
  • https://www.geeksforgeeks.org/spring-boot-customize-the-jackson-objectmapper/
This post is licensed under CC BY 4.0 by the author.