英文:
How to return a list of multiple JSON items in java, part 2
问题
这个问题是这个问题的后续。
再次强调,我完全不熟悉Java和JSON,所以我不太了解。我能够从上面提到的问题的答案中进一步前进一点,但现在我遇到了更多的问题,主要是如何返回多个JSON的语法问题。 (我从SpringBoot获取了这些代码,我真的不知道它是如何工作的;我还在学习Java。)
目前,这是我尝试的内容:
@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));
}
但我不知道如何在上面的代码中返回列表,在我试图获取结果的那一行:
LookupResponseResultList result = ldapMapper.toLookupResponseResultList(ldapModel.get());
ldapMapper.toLookupResponseResultList的代码如下:
public LookupResponseResultList toLookupResponseResultList(List<LDAPModel> ldapModel) {
return LookupResponseResultList.builder()
.userId(ldapModel.toString())
// .userId(ldapModel.getUserId())
// .telephoneNumber(ldapModel.getTelephoneNumber())
.build();
}
我想要的是两行注释的内容。我可以在注释的上一行看到整个JSON结构,如下所示:
{
"result": {
"userId": "[LDAPModel(userId=abcde123,telephoneNumber=1-555-5555555), LDAPModel(userId=fghi456,telephoneNumber=1-333-3333333)]",
},
"error": null
}
如何使toLookupResponseResultList返回多个JSON的列表(如下所示),而不是一个字符串?
{
"result": [
{
"userId": "abcde123",
"telephoneNumber": "1-555-5555555"
},
{
"userId": "fghi456",
"telephoneNumber": "1-333-3333333"
}
],
"error": null
}
编辑:LookupResponseResultList类如下所示:
public class LookupResponseList extends BaseBodyResponse<LookupResponseList.LookupResponseResultList> {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class LookupResponseResultList {
String userId;
String telephoneNumber;
}
}
英文:
This question is a follow up of [this question ](https://stackoverflow.com/questions/63256209/how-to-return-a-list-of-multiple-json-items-in-java "How to return a list of multiple JSON items in 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());
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();
}
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
}
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
}
Edit: LookupResponseResultList class is below:
public class LookupResponseList extends BaseBodyResponse<LookupResponseList.LookupResponseResultList> {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class LookupResponseResultList {
String userId;
String telephoneNumber;
}
答案1
得分: 3
The problem is at -
public LookupResponseResultList toLookupResponseResultList(List<LDAPModel> ldapModel) {
return LookupResponseResultList.builder()
.userId(ldapModel.toString()) // 此行
.build();
}
This LookupResponseResultList class must accept a List of LDAPModel objects.
class LookupResponseResultList {
@JsonProperty
private List<LDAPModel> result;
private String error.
}
Modify your builder and instead of setting a string using ldapModel.toString(), provide the list itself.
Currently, your LookupResponseResultList class is having a string representation of List<LDAPModel>. Provide the list instead of string.
英文:
The problem is at -
public LookupResponseResultList toLookupResponseResultList(List<LDAPModel> ldapModel) {
return LookupResponseResultList.builder()
.userId(ldapModel.toString()) // THIS LINE
.build();
}
This LookupResponseResultList class must accept a List of LDAPModel objects.
class LookupResponseResultList {
@JsonProperty
private List<LDAPModel> result;
private String error.
}
Modify your builder and instead of setting a string using ldapModel.toString(), provide the list itself.
Currently, your LookupResponseResultList class is having a string representation of List<LDAPModel>. Provide the list instead of string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论