[英]Serailize list of generic type List<T> for RestController in java using jackson
我想知道是否可以將泛型類型列表作為 controller 方法的返回。我試過了,但我遇到了 jackson 的序列化問題。 有什么解決辦法嗎? 這是我的方法:
@GetMapping(path = "parse", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<T> parseFichier(
@RequestParam String processus,
@RequestParam String nomFichier) throws ReaderFichierGdaException, ParsingProblemException, IOException {
List<T> test = this.reader.parser(nomFichier, ConstantesNuc.XLSX_EXT);
return test;
}
您在方法語法中缺少類型參數
@GetMapping(path = "parse", produces = { MediaType.APPLICATION_JSON_VALUE })
public <T> List<T> parseFichier(
@RequestParam String processus,
@RequestParam String nomFichier) throws ReaderFichierGdaException, ParsingProblemException, IOException {
List<T> test = this.reader.parser(nomFichier, ConstantesNuc.XLSX_EXT);
return test;
}
但是添加它並不能解決問題,會出現編譯時錯誤說
不存在類型變量的實例,因此類型符合 T 推理變量 E 具有不兼容的邊界:等式約束:T 下界:類型
如果你想返回任何類型的可序列化 object 我會推薦使用通配符?
或Object
@GetMapping(path = "parse", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<?> parseFichier(
@RequestParam String processus,
@RequestParam String nomFichier) throws ReaderFichierGdaException, ParsingProblemException, IOException {
List<T> test = this.reader.parser(nomFichier, ConstantesNuc.XLSX_EXT);
return test;
}
@GetMapping(path = "parse", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Object> parseFichier(
@RequestParam String processus,
@RequestParam String nomFichier) throws ReaderFichierGdaException, ParsingProblemException, IOException {
List<T> test = this.reader.parser(nomFichier, ConstantesNuc.XLSX_EXT);
return test;
}
如果您對父 class 有任何特定的有界可序列化 class 您也可以使用上限
@GetMapping(path = "parse", produces = { MediaType.APPLICATION_JSON_VALUE })
public List<? extends Parent> parseFichier(
@RequestParam String processus,
@RequestParam String nomFichier) throws ReaderFichierGdaException, ParsingProblemException, IOException {
List<T> test = this.reader.parser(nomFichier, ConstantesNuc.XLSX_EXT);
return test;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.