简体   繁体   English

必需的字符串参数“ text”不存在

[英]Required String parameter 'text' is not present

I'm trying to send a string from my view to my controller, but I keep getting the error that "text" is not present. 我正在尝试将字符串从视图发送到控制器,但我不断收到“文本”不存在的错误。

this is my javascript 这是我的javascript

$scope.sendMsg = function(){
        console.log($scope.my.message);

        data = {"text" : $scope.my.message};

        $http({
            method:'POST',
            data:data,
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            url:'/post-stuff'
        }).then(function(response){
            console.log(response);
        });     
    }

My rest controller : 我的休息控制器:

@RequestMapping(value= "/post-stuff", method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<PostTextResult> postStuff(@RequestParam(value= "text") String text){
        System.out.println(text);
        return new ResponseEntity<PostTextResult>(stuff.postTextContent(text), HttpStatus.OK);
    }

Not sure whether there is a way that Spring can de-json {"text" : "some text"} into the raw string "some text" . 不确定Spring是否可以将json {"text" : "some text"}为原始字符串"some text" However @RequestParam is used for parameters within URLs ( http://foo.com/post-stuff?text=text+here ). 但是,@ @RequestParam用于URL中的参数( http://foo.com/post-stuff?text=text+here )。

POST ed data is sent in the body, so you'll need to use the @RequestBody annotation. POST ed数据在正文中发送,因此您需要使用@RequestBody批注。 And since the body is a more complex type, I'd use a class that makes it easy to extract the value: 而且由于body是更复杂的类型,因此我将使用一个类来简化提取值的过程:

static class TextHolder {
  private String text;  // < name matters
  // getter+setter omitted
}

public ResponseEntity<PostTextResult> postStuff(@RequestBody TextHolder text) {
    System.out.println(text.getText());
    return new ResponseEntity<PostTextResult>(stuff.postTextContent(text.getText()), HttpStatus.OK);
}

You can try to change in frontend in your current data to: 您可以尝试将当前data前端更改为:

data = {params:{"text" : $scope.my.message}};

Spring need to know that request has parameters. Spring需要知道该请求具有参数。

Or you can try to change the in backend in your controller @RequestParam(value= "text") String text to @RequestBody String text 或者您可以尝试将控制器@RequestParam(value= "text") String text的in后端更改为@RequestBody String text

Check out the difference between RequestBody and RequestParam 看看RequestBody和RequestParam之间的区别

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 所需的字符串参数“graphName”不存在 - Required String parameter 'graphName' is not present 所需的字符串参数不存在:与 fetch 通信 - Required String parameter is not present : communicate with fetch Spring Boot REST - 所需的字符串参数不存在 - Spring Boot REST - Required String parameter is not present 从 ajax 向 spring controller 发送数据:所需的字符串参数不存在 - sending data from ajax to spring controller: Required String parameter is not present 如何从 java 脚本 function 中调用 spring 服务 (RequestParam)(错误 400:必需的字符串参数“标志”不存在) - How to call spring service (RequestParam ) from in java script function (Error 400: Required String parameter 'flag' is not present) Android Post to API无效,Ajax Post正常。 必需的字符串参数“名字”不存在” - Android Post to API Doesn't Work, Ajax Post Works OK. Required String parameter 'firstname' is not present" 发布错误必需的MultipartFile []参数不存在 - post error Required MultipartFile [ ] parameter not present POST请求-字符串参数不存在 - POST request - String parameter is not present 处理程序执行导致异常:所需的MultipartFile参数&#39;file&#39;不存在 - Handler execution resulted in exception: Required MultipartFile parameter 'file' is not present 仅当存在查询字符串参数时初始化媒体播放器 - Initialize media player only if query string parameter is present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM