Spring boot RequestBody getting null

Viewed 2214

I'm new in Spring Boot, i have a little issue when i'm getting JSON in my RestController from Postman.

When i send the request from Postmat with its attributes it's always getting null in the RequestBody.

This is my 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
          ...
    }
}

My RequestAdicion is like this:

@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;
}

I'm sending these attributes in 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"
}

I tried to set the first character in lowercase but it doesn't work.

This is the exception raised:

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

Thank you all in advance.

EDIT: I solved the problem setting the annotation @JsonProperty to each field, and creating to each model a NoArgsConstructor initializing variables with no value and the annotation @AllArgsConstructor in the class.

2 Answers

I just had the same issue for a different reason, so I'm adding this quick note to maybe help someone else. I couldn't figure why my controller parameter annotated as @RequestBody was coming thru as all blank/empty. Already checked Content-Type: application/json.

I wasted a lot of time on this before realizing my IDE had chosen a totally wrong import for that annotation class. So just make sure you've got

import org.springframework.web.bind.annotation.*;

and not some other RequestBody class in your imports.

The problem is with your case. AFAIK, by default spring uses camelCase even if you use PascalCase (or UpperCamelCase) for your fields.

I suggest you using camelCase in stead, but if you need to stick to pascal case you should add: @JsonProperty("Header") on your header field (and do the same for other fields.

you could also tweak the objectMapper to do this without the annotations.

Related