[英]Spring boot RequestBody getting null
我是 Spring 引導的新手,當我從 Postman 在我的 RestController 中獲取 JSON 時,我遇到了一個小問題。
當我從 Postmat 發送帶有其屬性的請求時,它總是在 RequestBody 中獲得 null。
這是我的 Rest controller
@RestController
@RequestMapping(value="/api/enviar",produces = MediaType.APPLICATION_JSON_VALUE,
headers = {"content-type=application/json"})
@RequiredArgsConstructor
@Slf4j
public class EnviarAdicionService {
@Autowired
private final EnviarAdicionUseCase enviarAdicionUseCase;
@Autowired
private final GuardarLogUseCase guardarLogUseCase;
private final Gson gson = new Gson();
private String _statusCode;
private Date dateStart;
@PostMapping
public DatosAdicionResponse PostAdicionFondos(@RequestBody @Valid RequestAdicion requestAdicion){
//PostMapping logic
...
}
}
我的 RequestAdicion 是這樣的:
@Data
@NoArgsConstructor
public class RequestAdicion implements Serializable {
private final static String regExpression = "\\{\\[sw\\.,ñÑ]\\+\\$}";
@RequestHeaderValidation
private RequestHeader Header;
@CuentaValidation(NumeroIdentificacionName = "NumeroDocumentoCuentaOrigen", TipoIdentificacionName = "TipoDocumentoCuentaOrigen",
TipoCuentaDepositosName = "TipoCuentaDepositoOrigen", NumeroCuentaDepositoName = "NumeroCuentaDepositoOrigen",
EntidadCuentaName = "EntidadCuentaOrigen", TipoCuentaName = "TipoCuentaOrigen", NumeroCuentaName = "NumeroCuentaOrigen",
CodigoFondoName = "CodigoFondoCuentaOrigen", EntidadCuentaDepositoName = "EntidadCuentaDepositoOrigen")
private CuentaOrigen CuentaOrigen;
@CuentaFondoValidation(TipoIdentificacionName = "TipoDocumentoCuentaDestino", NumeroIdentificacionFondoName = "NumeroDocumentoCuentaDestino",
EntidadName = "EntidadCuentaDestino", CodigoFondoName = "CodigoFondoCuentaDestino",
NumeroFondoInversionName = "NumeroFondoInversionCuentaDestino")
private CuentaFondo CuentaDestino;
@FormaDePagoValidation
@Pattern(regexp = regExpression,message = "Valor no permitido. FormaDePago")
private String FormaDePago;
@ValorValidation
private double ValorAdicion;
@OficinaValidation
private long Oficina;
@CanalValidation
@Pattern(regexp = regExpression,message = "Valor no permitido. Canal")
private String Canal;
}
我在 Postman 中發送這些屬性
Headers
Content-Type: application/json
Body
{
"Header": {
"SystemId": "AW1371",
"MessageId": "1234567890120006",
"UserName": "autWakanda",
"Destination": {
"Name": null,
"NameSpace": null,
"Operation": null
}
},
"CuentaOrigen": {
"NumeroDocumentoCuentaOrigen": 8232166,
"TipoDocumentoCuentaOrigen": "1",
"TipoCuentaDepositoOrigen": "7",
"NumeroCuentaDepositoOrigen": "40673760005",
"EntidadCuentaOrigen": "00007",
"TipoCuentaOrigen": "7",
"NumeroCuentaOrigen": "40673760005",
"CodigoFondoCuentaOrigen": "123"
},
"CuentaDestino": {
"TipoDocumentoCuentaDestino": "1",
"NumeroDocumentoCuentaDestino": 1360740,
"NumeroFondoInversionCuentaDestino": "0021008106434090",
"EntidadCuentaDestino": "00532",
"CodigoFondoCuentaDestino": "21"
},
"FormaDePago": "72",
"ValorAdicion": 133000.31,
"Oficina": 3132,
"Canal": "SVE"
}
我試圖將第一個字符設置為小寫,但它不起作用。
這是引發的異常:
Caused by: java.lang.NullPointerException: Cannot invoke "co.com.bancolombia.model.requestheader.RequestHeader.getSystemId()" because the return value of "co.com.bancolombia.api.models.RequestAdicion.getHeader()" is null
謝謝大家。
編輯:我解決了將注釋@JsonProperty 設置為每個字段的問題,並為每個model 創建了一個NoArgsConstructor,它初始化了沒有值的變量和class 中的注釋@AllArgsConstructor。
問題在於你的情況。 AFAIK,默認情況下 spring 使用 camelCase,即使您在字段中使用 PascalCase(或 UpperCamelCase)。
我建議您改用 camelCase,但如果您需要堅持使用 pascal 大小寫,則應在 header 字段上添加: @JsonProperty("Header")
(並對其他字段執行相同操作。
您還可以調整 objectMapper 以在沒有注釋的情況下執行此操作。
我只是出於不同的原因遇到了同樣的問題,所以我添加了這個快速說明可能會幫助其他人。 我不明白為什么我的 controller 參數注釋為@RequestBody 會全部空白/空。 已檢查 Content-Type: application/json。
在意識到我的 IDE 為該注釋 class 選擇了完全錯誤的導入之前,我在這上面浪費了很多時間。 所以只要確保你有
import org.springframework.web.bind.annotation.*;
而不是您的導入中的其他一些 RequestBody class 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.