You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Post请求报错‘Required request body is missing’求助

Fixing "Required request body is missing" in Angular + Spring Boot Integration

Hey there, let's break down why you're hitting this error and how to fix it. The "Required request body is missing" message usually means Spring can't parse the JSON payload your Angular app is sending—either because the format is invalid, headers are missing, or there's a serialization mismatch between your frontend and backend models.

1. Explicitly set the Content-Type request header

Angular's HttpClient.post normally sets Content-Type: application/json automatically, but if you have HTTP interceptors in your project, they might be overriding this critical header. Let's set it explicitly to eliminate this variable:

// First import HttpHeaders at the top of your component
import { HttpClient, HttpHeaders } from '@angular/common/http';

// Update your testDatabase method
public testDatabase() {
  this.authorizationMessage = new AuthorizationMessage();
  // ... your existing code to populate authorizationMessage ...
  
  // Convert Date to ISO string (we'll cover this in the next step)
  this.authorizationMessage.transmissionDateAndTime = new Date().toISOString();

  const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
  return this.httpClient.post(
    `${this.usersUrl}authorizeTransaction`,
    this.authorizationMessage,
    { headers }
  ).subscribe();
}

2. Fix Date serialization mismatch

Your frontend is sending a raw Date object, which gets serialized into a complex nested object (with properties like day, hours, etc.)—but your backend expects a LocalDateTime type, which requires an ISO 8601 string format.

Frontend fix:

Convert the Date to an ISO string before sending it:

this.authorizationMessage.transmissionDateAndTime = new Date().toISOString();

Backend fix:

Add a @JsonFormat annotation to your LocalDateTime field to tell Spring exactly how to parse the incoming ISO string:

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;

// Inside your AuthorizationMessage class
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private LocalDateTime transmissionDateAndTime;

3. Verify model property types match

Let's cross-check your frontend and backend AuthorizationMessage classes for type mismatches that could break parsing:

  • Frontend transactionAmount (number) ↔ Backend Long: Ensure you're sending integers (no decimals like 45.5)
  • Frontend primaryAccountNumber (number) ↔ Backend Long: If your account number is longer than 2^53 (common for credit cards), switch the frontend type to string to avoid precision loss
  • Property names are identical: Good news, your property names (like carAcceptorName) match perfectly—no typos here!

4. Inspect the actual request payload

Open your browser's DevTools (F12), go to the Network tab, and find the POST request to /fraud/authorizeTransaction. Check the Request Payload section:

  • If it's empty: Double-check that this.authorizationMessage is fully populated before calling post
  • If it has nested objects for the date: That's the serialization issue we fixed in step 2

5. Confirm Spring's JSON parsing is working

Spring Boot's web starter includes Jackson (the default JSON parser) out of the box, but if you've customized message converters, make sure you haven't removed the default JSON converter. The @RequestBody annotation relies on this to parse incoming JSON data.

Try these steps one by one—most likely the date serialization or missing Content-Type header is the root cause.

内容的提问来源于stack exchange,提问作者Mitchel Abrahams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:05:23