简体   繁体   English

将简单的字符串数组从 REST 反序列化为列表<String>在 Java 中

[英]Deserialize Simple Array Of String from REST to List<String> in java

I am getting the JSON from frontend angular to API and trying to convert array of string to list of string in controller :我正在从前端角度获取 JSON 到 API,并尝试将字符串数组转换为控制器中的字符串列表:

GET : http://localhost:8080/rest/v1/health/getValues

{
    "page" : 2,
    "size" : 10,
    "assignedTo" : [ "user1@example.com", "user2@example.com" ],
    "status" : ["abc","def"]
}

I am trying to convert first into List in following way but unable to access it in @Rest:我试图通过以下方式首先转换为 List 但无法在@Rest 中访问它:

public class SearchDTO {
    @JsonProperty("page")
     public int page;

    @JsonProperty("size")
    public int size;

    @JsonFormat(shape = JsonFormat.Shape.ARRAY)
    @Getter
    @Setter
    class AssignedTo {
        private String assignedTo;
        @JsonCreator
        public AssignedTo(@JsonProperty("assignedTo") String assignedTo) {
            this.assignedTo = assignedTo;
        }
  }
    @JsonFormat(shape= JsonFormat.Shape.ARRAY)
    @Getter
    @Setter
    class Status {
        private String status;
        @JsonCreator
        public Status(@JsonProperty("status") String status) {
            this.status = status;
        }
        }

    @JsonFormat(shape= JsonFormat.Shape.ARRAY)
    @Getter
    @Setter
    class LogType {
        private String logType;
        @JsonCreator
        public LogType(@JsonProperty("logType") String logType) {
            this.logType = logType;
        }
    }
}

I am unable to access same in the @RestController Class我无法在 @RestController 类中访问相同的内容

@PostMapping(path = "getValue", consumes = "application/json", produces = "application/json")
    public ResponseEntity<ListDTO> getAll(@RequestBody SearchDTO searchMapping ){
        return ResponseEntity.ok(logPageSearchService.getLogs(searchMapping.page,
                logSearchMapping.size,// other variables here
                ));
    }

Use simple dto class remove json tag it will work使用简单的 dto 类删除 json 标签它会起作用

@RestController
class DemoController {
    @PostMapping(path = "/getValue", consumes = "application/json", produces = "application/json")
    public ResponseEntity<SearchDTO> getAll(@RequestBody SearchDTO searchMapping ){
        return ResponseEntity.ok(searchMapping);
    }

}

class SearchDTO {
    public int page;
    public int size;
    public String[] assignedTo;
    public String[] status;
    
    // getters / setters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM