【问题标题】:How to pass JSON Object and return Object from Spring rest controller如何传递 JSON 对象并从 Spring 休息控制器返回对象
【发布时间】:2020-12-21 22:03:00
【问题描述】:

我有一个如下的实体类:

 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

所以我猜我没有以正确的方式发送这些值

对于 POJO 的描述如下:

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_16.pdf","inputName":"98111"}

如果我在正文中发送 的 Map,如何将其作为对象发送?有没有其他方法可以将该对象发送到控制器?

【问题讨论】:

    标签: java json spring spring-restcontroller spring-rest


    【解决方案1】:

    要返回状态和对象,您可以尝试这样做:

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

    【讨论】:

    • 谢谢。我仍在学习如何使用 ResponseEntity。这看起来不错。您对输入也有任何建议吗?
    • 能否请您在出现错误的地方留下评论?
    • 预期为 BEGIN_OBJECT 但在第 1 行第 13 列路径 $.desc 为 STRING 预期为 BEGIN_OBJECT 但在第 1 行第 13 列路径 $.nameBytes 为 STRING
    • 这可能是您的问题的解决方案:stackoverflow.com/questions/28418662/…
    • 谢谢我使用编码器通过了。
    猜你喜欢
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2020-01-21
    • 2020-11-18
    • 1970-01-01
    • 2015-11-11
    相关资源
    最近更新 更多