Spring 没有@RequestBody 的@RequestMapping 方法
Spring @RequestMapping method without @RequestBody
在 Spring 引导控制器方法中,如何获取 POST 的主体?我见过的所有示例都使用@RequestBody。如何在不使用@RequestBody 的情况下获取正文?
我正在编写一种方法来处理 Slack 事件。当 Slack POST 是一个事件时,正文在 JSON 中并且通常包含一个 "user" 键。根据事件的类型,"user" 的值可以是字符串或对象。因此,我无法创建单个 Class 并写入
@RequestMapping(path = "/slackRequest", method = RequestMethod.POST)
public String handleSlackRequest(@RequestBody final SlackRequest slackRequest)
答案:实现@ChiDov建议的方法,解决方案是保留@RequestBody,import
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
定义用户字段(如果它是一个简单的字符串值,则定义一个新字段来存储 'user')为
@OneToOne
private SlackEventUser user;
private String slackUserId;
并将其 Setter 方法定义为
@JsonSetter("user")
public void setUser(JsonNode userNode) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (userNode.isObject()) {
SlackEventUser slackEventUser = mapper.convertValue(userNode, SlackEventUser.class);
this.user = slackEventUser;
} else {
String userString = mapper.convertValue(userNode, String.class);
this.slackUserId = userString;
this.user = null;
}
}
已更新:我会让你的 DTO 像 :
Class SlackRequest{
...
private String eventType;
private JsonNode user;
...
public String getUser(){
return user.asText();
}
}
在控制器中:
@RequestMapping(path = "/slackRequest", method = RequestMethod.POST)
public String handleSlackRequest(@RequestBody final SlackRequest slackRequest){
if(slackRequest.getEventType == "objectEvent"){
SomeObject user = mapper.convertValue(slackRequest.getUser(), SomeObject.class);
// do something with the object
}else{
// do something with the user string
}
}
从中获取灵感:How to deserialize dynamic field using Jackson?
在 Spring 引导控制器方法中,如何获取 POST 的主体?我见过的所有示例都使用@RequestBody。如何在不使用@RequestBody 的情况下获取正文?
我正在编写一种方法来处理 Slack 事件。当 Slack POST 是一个事件时,正文在 JSON 中并且通常包含一个 "user" 键。根据事件的类型,"user" 的值可以是字符串或对象。因此,我无法创建单个 Class 并写入
@RequestMapping(path = "/slackRequest", method = RequestMethod.POST)
public String handleSlackRequest(@RequestBody final SlackRequest slackRequest)
答案:实现@ChiDov建议的方法,解决方案是保留@RequestBody,import
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
定义用户字段(如果它是一个简单的字符串值,则定义一个新字段来存储 'user')为
@OneToOne
private SlackEventUser user;
private String slackUserId;
并将其 Setter 方法定义为
@JsonSetter("user")
public void setUser(JsonNode userNode) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (userNode.isObject()) {
SlackEventUser slackEventUser = mapper.convertValue(userNode, SlackEventUser.class);
this.user = slackEventUser;
} else {
String userString = mapper.convertValue(userNode, String.class);
this.slackUserId = userString;
this.user = null;
}
}
已更新:我会让你的 DTO 像 :
Class SlackRequest{
...
private String eventType;
private JsonNode user;
...
public String getUser(){
return user.asText();
}
}
在控制器中:
@RequestMapping(path = "/slackRequest", method = RequestMethod.POST)
public String handleSlackRequest(@RequestBody final SlackRequest slackRequest){
if(slackRequest.getEventType == "objectEvent"){
SomeObject user = mapper.convertValue(slackRequest.getUser(), SomeObject.class);
// do something with the object
}else{
// do something with the user string
}
}
从中获取灵感:How to deserialize dynamic field using Jackson?