java—如何从SpringREST控制器传递json对象和返回对象

nafvub8i  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(420)

我有一个实体类,如下所示:

public class InputData {

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

这是我的休息控制器:

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

我有两个问题:
如何将报告以及http状态作为响应发送回去?
如何将数据发送到控制器?
我创建了以下测试用例:

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

当我运行此测试时,测试用例失败,并且我能够指出问题:

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

所以我猜我没有以正确的方式发送这个值
说明如下:

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

下面是要发送的值:{“desc”:“org.restservice”。description@1213ffbc“,”outputtype“:”tiff“,”inputtype“:”pdf“,”namebytes“:”src/test/resources/sampledocuments/test\u 16.pdf“,”inputname“:”98111“}
如果在body中发送<string,string>的Map,如何将其作为object发送?有没有其他方法把那个对象发送到控制器?

uwopmtnx

uwopmtnx1#

要返回状态和对象,可以尝试如下操作:

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

相关问题