如何在Java中返回多个JSON项的列表,第2部分

huangapple go评论74阅读模式
英文:

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(&quot;reports/{userID}&quot;)
public ResponseEntity&lt;LookupResponseList&gt; getDirectReports(@PathVariable String userID) {
    Optional&lt;List&lt;LDAPModel&gt;&gt; ldapModel = ldapService.getDirectReports(userID);
        if (!ldapModel.isPresent()) {
            return new ResponseEntity&lt;&gt;(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&lt;LDAPModel&gt; 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:

{
    &quot;result&quot;: {
    &quot;userId&quot;: &quot;[LDAPModel(userId=abcde123,telephoneNumber=1-555-5555555), LDAPModel(userId=fghi456,telephoneNumber=1-333-3333333)]&quot;,
    },
    &quot;error&quot;: null
}

How can I make it so that toLookupResponseResultList return a list of multiple JSON (like below) instead of a string?

{
&quot;result&quot;: [
    {
      &quot;userId&quot;: &quot;abcde123&quot;,
      &quot;telephoneNumber&quot;: &quot;1-555-5555555&quot;
    },
    {
      &quot;userId&quot;: &quot;fghi456&quot;,
      &quot;telephoneNumber&quot;: &quot;1-333-3333333&quot;
    }
    ],
&quot;error&quot;: null

}

Edit: LookupResponseResultList class is below:

public class LookupResponseList extends BaseBodyResponse&lt;LookupResponseList.LookupResponseResultList&gt; {
@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&lt;LDAPModel&gt; 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&lt;LDAPModel&gt; 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.

huangapple
  • 本文由 发表于 2020年8月6日 10:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63276147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定