不带@RequestBody的Spring@RequestMapping方法

不带@RequestBody的Spring@RequestMapping方法,spring,spring-boot,slack-api,Spring,Spring Boot,Slack Api,在Spring引导控制器方法中,如何获取帖子的主体?我看到的所有示例都使用@RequestBody。如果不使用@RequestBody,如何获取正文 我正在写一个处理空闲事件的方法。Slack发布事件时,主体是JSON格式的,通常包含一个“用户”键。根据事件的类型,“user”的值可以是字符串或对象。因此,我无法创建单个类并编写 @RequestMapping(path = "/slackRequest", method = RequestMethod.POST) public Stri

在Spring引导控制器方法中,如何获取帖子的主体?我看到的所有示例都使用@RequestBody。如果不使用@RequestBody,如何获取正文

我正在写一个处理空闲事件的方法。Slack发布事件时,主体是JSON格式的,通常包含一个“用户”键。根据事件的类型,“user”的值可以是字符串或对象。因此,我无法创建单个类并编写

@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;
将用户字段(如果是简单的字符串值,则为存储“用户”的新字段)定义为

并将其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;
    }
}

更新:我想让您的数据如下:

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
   }
}

灵感来自:

更新版::我会让您的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
   }
}

灵感来自:

只需接受
字符串
并根据内容手动映射即可?@darrenforsyth反序列化程序知道“{”是对象的开始,而不是字符串:“无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeTableException:无法读取文档:无法从START_对象标记中反序列化java.lang.String的实例”只需接受
字符串
,并根据内容手动映射即可?@darrenforsyth反序列化程序知道”{是对象的开始,而不是字符串:“读取HTTP消息失败:org.springframework.HTTP.converter.httpMessageNodeTableException:无法读取文档:无法从start_对象标记中反序列化java.lang.String的实例”处理程序从未被调用。反序列化程序知道“{”是一个对象的开始,而不是字符串:“读取HTTP消息失败:org.springframework.HTTP.converter.httpMessageNodeReadableExc‌​eption:无法读取文档:无法反序列化START\u对象标记中的java.lang.String实例“谢谢。我将研究1)非类型化”映射和2)自定义反序列化器不客气,希望我的回答能对你有所帮助。如果slackRequest中没有太多字段,我建议你使用自定义反序列化器。因此你可以根据eventType将其反序列化为字符串或对象。“非类型”映射:处理程序永远不会被调用。反序列化器知道“{”是一个对象的开始,而不是字符串:“读取HTTP消息失败:org.springframework.HTTP.converter.httpMessageNodeReadableExc‌​eption:无法读取文档:无法反序列化START\u对象标记中的java.lang.String实例“谢谢。我将研究1)非类型化”映射和2)自定义反序列化器不客气,希望我的回答对您有所帮助。如果slackRequest中没有太多字段,我建议您使用自定义反序列化器。因此,您可以根据eventType将其反序列化为字符串或对象。“非类型化”映射: