如何在Java中返回多个JSON项的列表,第2部分 [英] How to return a list of multiple JSON items in java, part 2
问题描述
此问题是此问题中返回多个JSON项目的列表.
同样,我对Java和JSON完全陌生,所以我一点也不了解.我能够进一步解决上述问题,但是现在我遇到了更多问题,即(基本上)如何返回多个JSON列表的语法.(我从SpringBoot获得了这些代码,但我真的不知道它们是如何工作的;我仍在学习Java.)
Again, I am completely new to Java and JSON, so I don't know much at all. I was able to proceed a little further with the answers from the question mentioned above, but now I have encountered more issues, namely (basically) the syntax of how to return the list of multiple JSON. (I got these codes from SpringBoot, and I really don't know how it all works; I am still learning Java.)
目前,这是我尝试过的:
Currently, this is what I've tried:
@GetMapping("reports/{userID}")
public ResponseEntity<LookupResponseList> getDirectReports(@PathVariable String userID) {
Optional<List<LDAPModel>> ldapModel = ldapService.getDirectReports(userID);
if (!ldapModel.isPresent()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
LookupResponseResultList result = ldapMapper.toLookupResponseResultList(ldapModel.get());
return ResponseEntity.ok(LookupResponseList.result(result, LookupResponseList.class));
}
但是我不知道如何在上面的代码中,在我试图获得结果的那一行上返回列表:
But I don't know how to return the list in the code above, on the line where I am trying to get the result:
LookupResponseResultList result = ldapMapper.toLookupResponseResultList(ldapModel.get());
ldapMapper.toLookupResponseResultList 的代码如下:
The code for ldapMapper.toLookupResponseResultList is below:
public LookupResponseResultList toLookupResponseResultList(List<LDAPModel> ldapModel) {
return LookupResponseResultList.builder()
.userId(ldapModel.toString())
// .userId(ldapModel.getUserId())
// .telephoneNumber(ldapModel.getTelephoneNumber())
.build();
}
两条要注释的行是我想要的.我可以在注释行上方的行中看到整个JSON结构,即:
The two commented line is what I want. I can see the entire JSON structure in the line above the commentted line, which is this:
{
"result": {
"userId": "[LDAPModel(userId=abcde123,telephoneNumber=1-555-5555555), LDAPModel(userId=fghi456,telephoneNumber=1-333-3333333)]",
},
"error": null
}
如何使toLookupResponseResultList返回多个JSON列表(如下所示)而不是字符串?
How can I make it so that toLookupResponseResultList return a list of multiple JSON (like below) instead of a string?
{
"result": [
{
"userId": "abcde123",
"telephoneNumber": "1-555-5555555"
},
{
"userId": "fghi456",
"telephoneNumber": "1-333-3333333"
}
],
"error": null
}
LookupResponseResultList类在下面:
LookupResponseResultList class is below:
public class LookupResponseList extends BaseBodyResponse<LookupResponseList.LookupResponseResultList> {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class LookupResponseResultList {
String userId;
String telephoneNumber;
}
推荐答案
问题在 -
public LookupResponseResultList toLookupResponseResultList(List<LDAPModel> ldapModel) {
return LookupResponseResultList.builder()
.userId(ldapModel.toString()) // THIS LINE
.build();
}
此LookupResponseResultList类必须接受LDAPModel对象的列表.
This LookupResponseResultList class must accept a List of LDAPModel objects.
class LookupResponseResultList {
@JsonProperty
private List<LDAPModel> result;
private String error.
}
修改您的构建器,而不是使用ldapModel.toString()设置字符串,而是提供列表本身.
Modify your builder and instead of setting a string using ldapModel.toString(), provide the list itself.
当前,您的LookupResponseResultList类具有List的字符串表示形式.提供列表而不是字符串.
Currently, your LookupResponseResultList class is having a string representation of List. Provide the list instead of string.
这篇关于如何在Java中返回多个JSON项的列表,第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!