[英]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.在这之后出现的任何错误,基本上都不是因为你发送的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.