0

I am trying to print the fields of a class. The class is as follows:

package com.telstra.sdwan.portal.model.neo4j;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;

import java.util.HashMap;

@NodeEntity
@Getter
@Setter
@NoArgsConstructor
public class Metrics {

    @Id
    @GeneratedValue
    public Long id;
    public String name;
    public HashMap<String, Long> success = new HashMap<String, Long>();
    public HashMap<String, Long> failure = new HashMap<String, Long>();

    public Metrics(String name) {
        this.name = name;
        success.put(name, 0L);
        failure.put(name, 0L);
    }
}

On running the API in the postman I am getting this error:

{
    "timestamp": "2021-01-27T16:28:44.5161857",
    "status": "BAD_REQUEST",
    "errors": [
        "Malformed JSON request"
    ],
    "message": "Required request body is missing: public org.springframework.http.ResponseEntity<com.telstra.sdwan.portal.model.neo4j.Metrics> com.telstra.sdwan.portal.controller.ConfigOpsAppController.apiCalled(com.telstra.sdwan.portal.model.neo4j.Metrics)",
    "data": [],
    "path": "/api/configops-app/metrics"
}

The Controller Method

@ApiOperation("Count of the number of times an API is called")
@GetMapping(value="/metrics")
@PreAuthorize("hasRole('User')")
public ResponseEntity<Metrics> apiCalled(@RequestBody Metrics metrics){ 
    return configOpsAPIService.apiCount(metrics);
}

Please suggest how this error can be resolved.

jeffrey.d.m
  • 110
  • 1
  • 10
Shreya
  • 11
  • 4
  • It looks like you have some Parameter in your Controller that expects to be passed through the request body (something like "@RequestBody Metrics metric"), and your Postman request does not include any However, it's important to add all relevant information in your question. Without seeing your Controller, or your Postman request it's impossible answer the question – jeffrey.d.m Jan 27 '21 at 11:29
  • Post the method signature of: `ConfigOpsAppController#apiCalled` – Aniket Sahrawat Jan 28 '21 at 00:58
  • Aniket Sahrawat I have added the method signature in the description – Shreya Jan 28 '21 at 07:21
  • 1
    @Shreya `GET` request must not contain a body. Although it should not have any effect on the working, it's not a recommended practice because you don't know which application might be stripping the body off. Make sure to rename `@GetMapping` to `@PostMapping` and your request body should look like: `{ "id": 0, "name": "some name", "success": {}, "failure": {} }` – Aniket Sahrawat Jan 28 '21 at 12:27
  • @Shreya You should also consider reading https://stackoverflow.com/a/983458/6099347 – Aniket Sahrawat Jan 28 '21 at 12:28

0 Answers0