【问题标题】:How to get raw JSON body in Spring REST controller?如何在 Spring REST 控制器中获取原始 JSON 正文?
【发布时间】:2020-12-12 16:48:56
【问题描述】:

下面的 API 接受来自客户端的 json 字符串,并将其映射到电子邮件对象中。如何获取请求正文 (email) 作为原始字符串? (我想要email 参数的原始字符串和类型版本)

PS:这个问题不是重复的:How to access plain json body in Spring rest controller?

@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody Email email) {
    //...
    return new ResponseEntity<>(HttpStatus.OK);
}

【问题讨论】:

  • 你试过使用 toString 或 StringBuilder 方法吗?
  • 为什么,正是,它不是重复的吗?
  • @chrylis-cautiouslyoptimistic- 另一个问题是关于只获取原始字符串,而不关心参数的类型化(映射)版本
  • @kalpajagrawalla 我认为这会导致一些开销,因为我们必须将对象转换回 JSON 字符串
  • 你试过把两个都放了吗?

标签: java spring spring-boot spring-mvc


【解决方案1】:

Spring 在后面为此使用了 Jackson,您可以使用它来将其序列化为字符串。像这样:

@Autowired private ObjectMapper jacksonMapper;

@PostMapping(value = "/mailsender")
public ResponseEntity<Void> sendMail(@RequestBody Email email) {
    //...
    log.info("Object as String: " + jacksonMapper.writeValueAsString(email));
    return new ResponseEntity<>(HttpStatus.OK);
}

【讨论】:

    【解决方案2】:

    您可以使用 GSON 库创建字符串类型的 json

    Gson gson = new Gson();
    
    @PostMapping(value = "/endpoint")
    public ResponseEntity<Void> actionController(@RequestBody Car car) {
        //...
        log.info("Object as String: " + this.gson.toJson(car));
        return new ResponseEntity<>(HttpStatus.OK);
    }
    

    【讨论】:

      【解决方案3】:

      我没有得到关于这个问题的所有信息,但我试着按照我的理解来回答。好吧, 如果您想获取请求正文:

      • 正如你所说的How to access plain json body in Spring rest controller? 这里已经写了如何做到这一点。如果出现问题,可能您在Email 类中发送了错误的 json 或不合适的类型。也许您的请求来自 url 过滤器

      • 第二种方法试试这样:

        private final ObjectMapper mapper = new ObjectMapper();
        
        @PostMapping(value = "/mailsender")
        public ResponseEntity<Void> sendMail(HttpServletRequest req) {
            // read request body
            InputStream body = req.getInputStream();        
            byte[] result = ByteStreams.toByteArray(body);
            String text =new String(result,"UTF-8");
            //convert to object
            Email email = mapper.readValue(body, Email .class);
            return new ResponseEntity<>(HttpStatus.OK);
        }
        
        

      如果要将对象转换为json字符串read this post

      【讨论】:

        【解决方案4】:

        你可以用不止一种方法,列出两种

         1. **Taking string as the paramater**,
             @PostMapping(value = "/mailsender")
                public ResponseEntity<Void> sendMail(@RequestBody String email) {
                    //... the email is the string can be converted to Json using new JSONObject(email) or using jackson.
                    return new ResponseEntity<>(HttpStatus.OK);
                }
        
         2. **Using Jackson** 
                 @PostMapping(value = "/mailsender")
                    public ResponseEntity<Void> sendMail(@RequestBody Email email) {
                        //...
                        ObjectMapper mapper = new ObjectMapper(); 
                        String email = mapper.writeValueAsString(email); //this is in string now
                        return new ResponseEntity<>(HttpStatus.OK);
                    }
        

        【讨论】:

        • 第一种方式是我现在使用的解决方案,但希望Spring能提供更好的方式来解决这个问题。
        • 第一个:email是一个编码的表单字符串,是spring修改的
        猜你喜欢
        • 2018-10-04
        • 2019-08-15
        • 1970-01-01
        • 2018-04-08
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多