繁体   English   中英

Spring 启动 RequestBody 获取 null

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM