0

I'm returning a list of error messages using a ResponseEnitiy body method. Altough I'm not sure why it's returning header object on the top of the reponse Ex:

[
{
    "headers": {},
    "body": {
        "code": 6,
        "description": "Billing account must not be null!.",
        "tcn": "c6fcaa15-bc68-4dc9-87b8-2722ea3e6a20",
        "personId": "1087280192",
        "errorDetails": null,
        "billingAccountsDTO": {
            "billingAccountNumber": "",
            "debtAmount": 1000.0,
            "msisdn": "0554160163",
            "hasProblem": true
        }
    },
    "statusCode": "BAD_REQUEST",
    "statusCodeValue": 400
},
{
    "headers": {},
    "body": {
        "code": 8,
        "description": "Msisnd must not be invalid or null!.",
        "tcn": "c6fcaa15-bc68-4dc9-87b8-2722ea3e6a20",
        "personId": "1087280192",
        "errorDetails": null,
        "billingAccountsDTO": {
            "billingAccountNumber": "STC234562",
            "debtAmount": 0.0,
            "msisdn": "05541x60163",
            "hasProblem": true
        }
    },
    "statusCode": "BAD_REQUEST",
    "statusCodeValue": 400
}

]

I want to get rid of the heasers object. The wierd behavour is that when I'm not returning a list of responses. the headers dissapeare !!!

napshot of the implementation of the response :

Response buildResponse(String tcn, ResponseCode responseCode, String personId, BillingAccountsDTO billingAccount) {
Response response =
    new Response(responseCode.getCode(), responseCode.getDescription(), tcn, personId, billingAccount,null);
return response;

}

   response = ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(buildResponse(tcn, ResponseCode.INVALID_DEBT_AMOUNT, billingAccountDTO.getPersonId(), billingAccountsDTO));
        auditLog.setRequestBody(billingAccountDTO.toString());
        auditLog.setResponseCode(ResponseCode.INVALID_DEBT_AMOUNT.getCode());
        billingAccountsDTO.setHasProblem(true);
        responseList.add(response);
      }
TechNewPy
  • 23
  • 4
  • You are adding the `ResponseEntity` instances to the list. Which is probably not wahat you actually want to to. You probably want to add the list or responses (build from `buildResponse` to be added to the list and that list to be part of the `ResponseEntity`. – M. Deinum Dec 21 '22 at 13:06
  • What's wrong with adding the responses to the list ? Is this why I'm keeping getting the headers object ? – TechNewPy Dec 21 '22 at 13:19
  • You are adding the web support clas for `ResponseEntity` to the list, so yes that is wrong, as the `ResponseEntity`, as the name implies, contains everything needed for Spring MVC to create the response. YOu want one response (one status, one set of headers). What you want is probably the content of the body which is what you are building in the `buildResponse` method, that is what you should add to the list **not** the `ResponseEntity`. You stuff it all in the list, set the List as the body of the response and you will get 1 set of headers, status with a listed errors. – M. Deinum Dec 21 '22 at 13:54
  • can you post a more complete snippet? what is the type of that `responseList` object...also, what is the correlation between the above `response` with below `response`? If `Response` class is your defined class, why did you assign `response` instance to a `ResponseEntity` object on below snippet? – cipley Dec 21 '22 at 14:16

0 Answers0