简体   繁体   English

Spring-Boot REST - 不存在必需的String参数

[英]Spring-Boot REST - Required String parameter is not present

So, I'm trying to to a POST request to a local Spring MVC project, but every time I try, I keep getting 400: Bad Request, with the error specified in the title. 所以,我正在尝试向本地Spring MVC项目发出POST请求,但每次尝试时,我都会收到400:Bad Request,标题中指定了错误。 Not sure why, because when I check what's being sent, it's correct. 不知道为什么,因为当我检查发送的是什么时,它是正确的。

Spring REST controller request Spring REST控制器请求

@RequestMapping(value="/add", method = RequestMethod.POST)
void addPet(@RequestParam String name, @RequestParam String photo, @RequestParam String status) {
    Pet pet = new Pet(name, photo, status);
    this.petRepo.save(pet);
}

AngularJS request sending the data AngularJS请求发送数据

$http({
       method: "post",
       url: "http://localhost:8080/pet/add",
       data:{
           name: angular.element("#name")[0].value,
           photo: angular.element("#photo")[0].value,
           status: angular.element("#status")[0].value
       }
    }).success(function(data, status) {

        })
        .error(function(data, status) {
            alert("Error");
            console.log(data);
            console.log(status);
        });

front-end HTML that's calling the AngularJS function 调用AngularJS函数的前端HTML

<div class="form-group">
        <label for="name">Pet Name</label>
        <input type="text" class="form-control" id="name">
    </div>
    <div class="form-group">
        <label for="photo">Photo URL</label>
        <input type="text" class="form-control" id="photo">
    </div>
    <div class="form-group">
        <label for="status">Status</label>
        <input type="text" class="form-control" id="status" placeholder="Something descriptive like 'Available' or 'Taken'">
    </div>
    <div class="form-group">
        <div class="btn-group">
            <button type="button" class="btn btn-primary" ng-click="preview()">Preview</button>
            <button type="button" class="btn btn-success" ng-click="submit()">Submit</button>
            <button type="button" class="btn btn-danger" ng-click="clear()">Clear</button>
        </div>
    </div>

You are using @RequestParam in the backend but in angular you are putting the values in the body of the request. 您在后端使用@RequestParam但是在角度中您将值放在请求的正文中。

You have to use @RequestBody in the backend or adjust the frontend to put the values in url parameters. 您必须在后端使用@RequestBody或调整前端以将值放入url参数中。

Found the answer. 找到了答案。 I followed SSH's answer above for the front-end, and for the backend, I changed the function to: 我按照上面的SSH回答前端,对于后端,我将功能更改为:

void addPet(@RequestBody Pet pet)

try like this 试试这样

<div class="form-group">
      <label for="name">Pet Name</label>
<input type="text" class="form-control" ng-model="form.name" id="name">
   </div>
<div class="form-group">
    <label for="photo">Photo URL</label>
    <input type="text" class="form-control" ng-model="form.photo"  id="photo">
</div>
<div class="form-group">
    <label for="status">Status</label>
    <input type="text" class="form-control" ng-model="form.status"  id="status" placeholder="Something descriptive like 'Available' or 'Taken'">
</div>
<div class="form-group">
    <div class="btn-group">
        <button type="button" class="btn btn-primary" ng-click="preview()">Preview</button>
        <button type="button" class="btn btn-success" ng-click="submit(form)">Submit</button>
        <button type="button" class="btn btn-danger" ng-click="clear()">Clear</button>
    </div>
</div>

and in controller use this 在控制器中使用它

 $scope.form = {"name":"","photo":"","status":""}
 $http({
   method: "post",
   url: "http://localhost:8080/pet/add",
   data:{
       name: $scope.form.name,
       photo: $scope.form.photo,
       status: $scope.form.status
   }
}).success(function(data, status) {

    })
    .error(function(data, status) {
        alert("Error");
        console.log(data);
        console.log(status);
});

暂无
暂无

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

相关问题 必需的MultipartFile参数不存在 - Spring Boot REST POST - Required MultipartFile parameter is not present - Spring Boot REST POST spring-boot maven rest 应用程序需要哪些依赖项? - Which dependencies are required for a spring-boot maven rest application? 方法参数类型字符串的 Spring Boot 和 HTML 所需的请求参数“Coord1”不存在] - Spring Boot and HTML Required request parameter 'Coord1' for method parameter type String is not present] Spring boot 方法参数类型字符串所需的请求参数“paramName”不存在 - Spring boot Required request parameter 'paramName' for method parameter type String is not present Spring Controller必需的String参数不存在 - Spring controller Required String parameter is not present Spring MVC 中不存在所需的字符串参数错误 - Required String parameter is not present error in Spring MVC Spring RestTemplate不提供必需的String参数 - Required String parameter is not present with Spring RestTemplate 所需的字符串参数不存在 Spring MVC - Required String parameter is not present Spring MVC Java Spring启动控制器@RequestParam给出错误“必需字符串参数&#39;名称&#39;不存在” - Java Spring boot controller @RequestParam gives error “Required String parameter 'name' is not present” 使用 json 上传 Spring 引导文件中不存在必需的 FileUpload[] 参数“body” - Required FileUpload[] parameter 'body' is not present in Spring boot file upload with json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM