Accepting JSON Requests in Spring Boot 3 Regardless of Content-Type

I am currently developing an API using Spring Boot 3 with a POST endpoint that utilizes @RequestBody to receive requests. However, I’ve encountered an issue: @RequestBody only processes requests with the Content-Type set to application/json.

I would like to configure my API to accept and process JSON data regardless of the Content-Type header. This means that even if the Content-Type is missing or set to something else (e.g., text/plain or application/x-www-form-urlencoded), I want to parse the request body as JSON into my RequestDto.

What would be the best approach to achieve this functionality in Spring Boot?

To achieve this, I attempted to replace @RequestBody with HttpServletRequest in my controller and manually convert the request body using ObjectMapper:

@PostMapping("/api")
public ResponseEntity<?> handleRequest(HttpServletRequest request) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    RequestDto dto = objectMapper.readValue(request.getInputStream(), RequestDto.class);
    // Proceed with processing
}

However, this approach has some drawbacks:

Validation Issues: I lose the ability to use the validation annotations (@Valid, @NotNull, etc.) that I have defined in my RequestDto. Implementing validation manually in the controller would complicate the code.

Code Maintainability: Writing custom parsing and validation logic in every controller method is not ideal.

Next, I tried using a filter to override the Content-Type header in incoming requests, hoping that Spring would process the request body as JSON regardless of the original Content-Type. Here’s the code I used in my filter:

public class ContentTypeOverrideFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequestWrapper servletRequestWrapper = new HttpServletRequestWrapper(
                (HttpServletRequest) servletRequest) {
            @Override
            public String getContentType() {
                return MediaType.APPLICATION_JSON_VALUE;
            }
        };

        chain.doFilter(servletRequestWrapper, servletResponse);
    }
}

This solution works when the Content-Type header is absent in the request. However, when the request has a Content-Type specified, the override does not take effect, and the request is not processed as JSON.

Question:

  • How can I configure my Spring Boot application to process JSON data in POST requests regardless of the Content-Type header, while still utilizing the validation annotations in my RequestDto?
  • Is there a way to globally override or ignore the Content-Type header for specific endpoints or for the entire application, so that all request bodies are treated as JSON?
  • Alternatively, is there a better approach to achieve this functionality without losing the benefits of @RequestBody and validation annotations?

New contributor

Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật