Skip to content
Advertisement

How to pass JSON Object and return Object from Spring rest controller

I have an entity class like below:

 public class InputData {

   byte[] nameBytes;
   InputType inputType;
   InputType outputType;
   String inputName;
   Description desc;
 }

Here is my rest controller:

   @PostMapping(path = "/submitData", consumes = "application/json")
   public HttpStatus callDataService(@RequestBody Map<String, String> json) {
    Gson gson = new GsonBuilder().create();
    InputData inputData = gson.fromJson(json.get("inputData"), InputData.class);
    Report report = dataService.getReport(inputData);
    //return HttpStatus.OK;
}

I have two questions:

How can I send the report as well as Http Status back as a response?

How to send the data to controller?

I have created the following test case:

@Test
public void testController() throws JSONException {

    Gson gson = new Gson();

    Description desc = new Description();
    desc.setMinimumValidSize(512);
    
    File file = new File("src/test/resources/sampleDocuments/test_1.pdf");

    byte[] byteArray = { 'P', 'A', 'N', 'K', 'A', 'J' };

    JSONObject inputSample = new JSONObject();
    inputSample.put("nameBytes", byteArray);
    inputSample.put("inputType", ImageType.PDF);
    inputSample.put("outputType", ImageType.TIFF);
    inputSample.put("inputName", "ABCDEF");
    inputSample.put("desc", desc);

    String  result = invokeRest(fileInputSample.toString(),"/submitData", HttpMethod.POST);
    assertEquals("200", result);
}

private String invokeRest(String basicParams, String inputImageType, String 
    outputImageType, String options, String url, HttpMethod httpMethod) {

    String testUrl = "http://localhost:" + port + url;

    Map<String, Object> body = new HashMap<>();
    body.put("fileInput", basicParams);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<String> entity = new HttpEntity(body, headers);
    String result = "";

    ResponseEntity<String> response = restTemplate.exchange(testUrl, httpMethod, entity, String.class);
    if (response.getStatusCode() == HttpStatus.OK) {
        result = response.getBody();
    } else {
        result = response.getStatusCode().toString();
    }
    return result;
}

When I run this the test case failed and I was able to pin point the issue :

Expected BEGIN_OBJECT but was STRING at line 1 column 13 path $.desc

So I am guessing I am not sending this values in right way

For Description POJO is below:

public class Description {
    private static final int DPI = 300;

    private Ctype c = CType.NONE;
    private ColorType color = DEFAULT_COLOR;
    private int dpi = DPI;
}

public enum CType {
    NONE, GROUPA,GROUPB,GROUPB_B,GROUPD
}


public enum ColorType {
    RGB, GREY;
}

Here is the values that is being send: {“desc”:”org.restservice.Description@1213ffbc”, “outputType”:”TIFF”,”inputType”:”PDF”,”nameBytes”:”src/test/resources/sampleDocuments/test_16.pdf”,”inputName”:”98111″}

How can I send that as Object if I am sending a Map of <String, String> in body? Is there any other way to send that object to controller?

Advertisement

Answer

To return the status and also the object you can try to do it like this:

@PostMapping(path = "/submitData", consumes = "application/json")
   public ResponseEntity<Report> callDataService(@RequestBody Map<String, String> json) {
    Gson gson = new GsonBuilder().create();
    InputData inputData = gson.fromJson(json.get("inputData"), InputData.class);
    Report report = dataService.getReport(inputData);
    return ResponseEntity.ok(report);
}
Advertisement