简体   繁体   English

我想将列表(它是对象的成员)从 Postman 发送到 Spring REST ZDB974238714CA8ACED638

[英]I want to send a List(which is a member of an object) from Postman to Spring REST API

I have an object, and of its attributes is a List.我有一个 object,它的属性是一个列表。 I want to send this object from Postman to my service.我想将此 object 从 Postman 发送给我的服务。 I'm using Spring 5.2.7 (Spring MVC, not SpringBoot) and Hibernate 5.4.17 and Java 8. My problem is very similar to this one: I want to send a Postman POST request with an Array: members: ["william", "eric", "ryan"] I'm using Spring 5.2.7 (Spring MVC, not SpringBoot) and Hibernate 5.4.17 and Java 8. My problem is very similar to this one: I want to send a Postman POST request with an Array: members: ["william ", "埃里克", "瑞恩"]

This is the class I'm trying to pass in Postman (POST method):这是我试图传入 Postman 的 class (POST 方法):

public class ChatDescriptionDto {

    private String chatID;

    private List<String> members;

    private String chatType;

    public String getChatID() {
        return chatID;
    }

    public void setChatID(String chatID) {
        this.chatID = chatID;
    }

    public List<String> getMembers() {
        return members;
    }

    public void setMembers(List<String> members) {
        this.members = members;
    }
    
    public void addMembers(List<String> members)
    {
        if(this.members == null)
            this.members = new ArrayList<>();
        this.members.addAll(members);
    }
    
    public void addMember(String member)
    {
        if(this.members == null)
            this.members = new ArrayList<>();
        this.members.add(member);
    }

    public String getChatType() {
        return chatType;
    }

    public void setChatType(String chatType) {
        this.chatType = chatType;
    }
}

I've tried this and it didn't work:我已经尝试过了,但它没有用:

{
    "chatID": "123",
    "members": ["P2001222833","P2001640916"],
    "chatType": "personal"
}

Edit: This is my controller:编辑:这是我的 controller:

@PostMapping("/initiateChat")
public String initiateChat(@RequestBody ChatDescriptionDto chat)
{
    return chatServiceLocal.initiateChat(chat)?"Chat Description created":"Failure! Could not save.";
}

Edit 2: The method which I've written in the question, "members": ["P2001222833","P2001640916"], is the correct one.编辑2:我在问题中写的方法"members": ["P2001222833","P2001640916"],是正确的。 Turns out, there was some error in the server so it never started and I didn't check that.事实证明,服务器中有一些错误,所以它永远不会启动,我没有检查。

Having no information about the Controller class you're using, the first thing I'd assume is that you're receiving an empty object, which means that Spring simply skipped the serialization. Having no information about the Controller class you're using, the first thing I'd assume is that you're receiving an empty object, which means that Spring simply skipped the serialization. This is the case when you don't specify the parameter of the method as @RequestBody .当您没有将方法的参数指定为@RequestBody时,就会出现这种情况。 First, make sure that you do have the annotation.首先,确保您确实有注释。

@RestController
@RequestMapping("/")
public class TestController {

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public ResponseEntity test(@RequestBody ChatDescriptionDto dto) {
        System.out.println(dto);
        return ResponseEntity.ok().build();
    }

}

If that's not the case, I'd assume that the problem is with the content type you're using.如果不是这种情况,我认为问题出在您使用的内容类型上。 Spring uses JSON by default, but you can change it in your endpoint's configuration. Spring 默认使用 JSON,但您可以在端点配置中更改它。

To send a simple object request, you do:要发送一个简单的 object 请求,您可以:

{ {

"member":"kola" “成员”:“可乐”

} }

To send a list object request, you do:要发送列表 object 请求,您可以:

{ {

"member": ["kola","wale","ojo"] "成员": ["kola","wale","ojo"]

} }

This is more like listing array elements.这更像是列出数组元素。

Any error that pops up after this, is basically not because of the request you sent.在这之后出现的任何错误,基本上都不是因为你发送的请求。

Postman 发送 JSON Object 包含列表<object><div id="text_translate"><p>我目前正在从事一个项目,在该项目中,其他人编写的当前实现采用包含可以解释为其他 4 种 Object 类型的列表的消息,如其 XmlElements 列表中所述。 我目前正在为此编写 Rest 实现,我的问题是 Postman 是否可以发送包含 Object 类型列表的消息正文。 当我发送一个原始的 JSON 主体进行测试时,我收到了我的 object,但它的列表是 null。</p><p> 这是只有一种类型的消息的示例。</p><pre class="lang-json prettyprint-override"> { "listObject": [ { "type1":{ "var1": "String", "var2": "String", "var3": "Instance", "var4": "String", "var5": "String", "var6": Integer, } } ] }</pre><p> 这是我接收帖子的代码</p><pre class="lang-java prettyprint-override">@RestController public class ExampleRestController{ @Autowired ServiceObject service; @PostMapping(value="path/example", produces=MediaType.APPLICATION_JSON_VALUE) public ResponseType example(@RequestBody ExampleObj obj){ return service.handleObj(obj); } } public class ExampleObj{ protected List&lt;Object&gt; listObject; public List&lt;Object&gt; getListObject{ if(listObject == null) listObject = new ArrayList&lt;Object&gt;(); return listObject; } }</pre><p> 在编写消息正文时,我确保使用我正在尝试测试的 object 的变量名称,但是当我测试时,我收到一个列表 null。</p><p> 是否可以使用 Postman 发送类型为 Object 的列表?</p></div></object> - Postman send JSON Object containing List<Object>

暂无
暂无

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

相关问题 邮差404 Spring Rest API - Postman 404 spring rest api 如何将时间和日期值从邮递员发送到我的 REST API - How to send Time and Date value from Postman to my REST API rest api - 如何在rest api调用中发送邮递员的身体参数 - rest api - how to send body parameters of postman in rest api call Spring Rest API的返回列表 - Return list from Spring rest api Postman 发送 JSON Object 包含列表<object><div id="text_translate"><p>我目前正在从事一个项目,在该项目中,其他人编写的当前实现采用包含可以解释为其他 4 种 Object 类型的列表的消息,如其 XmlElements 列表中所述。 我目前正在为此编写 Rest 实现,我的问题是 Postman 是否可以发送包含 Object 类型列表的消息正文。 当我发送一个原始的 JSON 主体进行测试时,我收到了我的 object,但它的列表是 null。</p><p> 这是只有一种类型的消息的示例。</p><pre class="lang-json prettyprint-override"> { "listObject": [ { "type1":{ "var1": "String", "var2": "String", "var3": "Instance", "var4": "String", "var5": "String", "var6": Integer, } } ] }</pre><p> 这是我接收帖子的代码</p><pre class="lang-java prettyprint-override">@RestController public class ExampleRestController{ @Autowired ServiceObject service; @PostMapping(value="path/example", produces=MediaType.APPLICATION_JSON_VALUE) public ResponseType example(@RequestBody ExampleObj obj){ return service.handleObj(obj); } } public class ExampleObj{ protected List&lt;Object&gt; listObject; public List&lt;Object&gt; getListObject{ if(listObject == null) listObject = new ArrayList&lt;Object&gt;(); return listObject; } }</pre><p> 在编写消息正文时,我确保使用我正在尝试测试的 object 的变量名称,但是当我测试时,我收到一个列表 null。</p><p> 是否可以使用 Postman 发送类型为 Object 的列表?</p></div></object> - Postman send JSON Object containing List<Object> 如何在 postman 中发送包含 arraylist 的 Object - How to send Object which contains an arraylist in the postman 使用Postman以POST方法发送嵌套的json对象到Spring REST API - Sending nested json object in POST method using Postman to Spring REST API 如何使用邮递员休息客户端发送对象以调用REST服务 - how to send object using postman rest client to call REST service 如何解决通过 Z03D476861AFD3854510 测试 spring 引导 rest api 时遇到的错误? - How can I solve the error I get while testing the spring boot rest api via postman? 尝试发送类对象列表作为对REST API的响应 - Trying to send list of class object as response to REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM