相关疑难解决方法(0)

Spring的@RequestBody在POST上提供空字符串

我有一个Spring 3.0.5.RELEASE的应用程序试图使用@RequestBody获取帖子的完整内容.调用该方法,但传递的字符串始终为空.我通过放置断点检查了StringHttpMessageConverter是否被调用,但内部HttpInputMessage是空的.

我已经看过Jetty和Tomcat的这个问题,所以我放弃了容器的问题.

这是我的样本控制器:

@Controller
@RequestMapping("/")
public class SubscriptionController {
    @RequestMapping(value = "/requestbody", method = RequestMethod.POST)
    public ModelAndView mycustomAction(@RequestBody String body) {

        // body is always empty
        Logger.getLogger(this.getClass()).debug("REQUEST BODY '" + body + "'");
        return new ModelAndView("empty");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序上下文定义如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Enable auto detection of controllers -->
    <context:component-scan base-package="com.big.viajerotelcel.controller" />

    <!--
        use annotation driven mvc and one single validator with JSR-303
        standard
    -->
    <mvc:annotation-driven …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

9
推荐指数
1
解决办法
1万
查看次数

如何在 Spring REST 控制器中获取原始 JSON 主体?

下面的 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);
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

spring ×2

spring-mvc ×2

spring-boot ×1