spring boot中使用@RequestBody MultiValueMap 总是报400,415错误

spring boot中使用@RequestBody MultiValueMap 总是报400,415错误
Content-Type为application/json时,报415错误
如下图所示

clipboard.png

Content-Type为application/x-www-form-urlencoded时,报400错误

clipboard.png

代码结构如下图:

clipboard.png

代码如下:

build.gradle

buildscript {
    ext {
        springBootVersion = '1.4.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-web-services')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

main类
DemoApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

controller类

package com.example.controller;

import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @RequestMapping(value = "/hello/{modelId}",method = RequestMethod.PUT)
    @ResponseStatus(value = HttpStatus.OK)
    public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap values) {
        System.out.println("hello:" + modelId);
    }
}

前端页面

<!DOCTYPE html>
<html lang="en" ng-app="hello">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body ng-controller="HelloCtrl">

<button ng-click="save()">http</button>

<script src="angular_1.2.13/angular.min.js"></script>
<script>
    var helloApp = angular.module("hello", []);
    helloApp.controller("HelloCtrl", function ($scope, $http) {
        $scope.save = function () {
            $http({
                      method: 'PUT',
                      data: {'name':'kaka'},
                      ignoreErrors: true,
                      headers: {
                          'Accept': 'application/json',
                          // 改为application/json时,会报415错误
                          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                      },
                      url: 'hello/xxx'
                  }).success(function (data, status, headers, config) {
                alert('ok');
            });
        };
    })
</script>
</body>
</html>
阅读 21.7k
2 个回答

'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 问题在这里 'application/x-www-form-urlencoded' 你传送的数据格式为json,但是请求头标志了为application/x-www-form-urlencoded这个,改为'Content-Type': 'application/json' 就可以了,不过会衍生其他问题,在使用cors跨域的时候会出现请求头附带的信息被打包到 request payload 里面.

新手上路,请多包涵

PUT转POST就可以了。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题