java - @RequestMapping(headers)、@RequestBody 不起作用

标签 java spring-boot axios

我在 @RequestMapping 注释中配置 header 属性时遇到问题。这是我的代码: HTML 页面:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
     <script type="text/javascript" th:src="@{/css/url_rearch_params.js}"/>
</head>
<body>
    <div id="app">
    <h1>TEST FORM</h1>
     <form action="" method="post">
        <p>Type description: <input type="text" v-model="type.description"/></p>
        <p><button v-on:click="addType()"> Send </button><input type="reset" value="Reset" /></p>
    </form>
    </div>


    <script src="http://cdn.jsdelivr.net/vue/1.0.10/vue.min.js"></script> 
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
      <script>
        Vue.prototype.$http = axios;
        new Vue({
            el:'#app',
            data:{
                type:{description:''}
            },
            methods:{
                addType(){
                    const config = { headers: { 'Content-Type': 'application/x-www-form-urlencoded',
                                                 'Accept': 'application/x-www-form-urlencoded'
                                              }
                                   };
                    let newType = {description:this.type.description};
                    console.log(newType);
                    this.$http.post('/types/insert',newType,config).then(response => {
                        console.log(response);
                    });
                }
            }
        });
      </script>
</body>
</html>

还有我的java代码:

@RequestMapping(value = "/insert",method = RequestMethod.POST, headers = {"Accept=application/x-www-form-urlencoded","Content-Type = application/x-www-form-urlencoded"},consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public @ResponseBody void createType(@RequestBody Type type) {
    System.out.println(type);
    typeService.createType(type);
}

如果我尝试执行该方法,则会出现以下问题:

There was an unexpected error (type=Not Found, status=404).

如果我删除:

headers = {“Accept=application/x-www-form-urlencoded”,“Content-Type = application/x-www-form-urlencoded”}

@requestpost 参数我有以下错误:

There was an unexpected error (type=Unsupported Media Type, status=415). Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported

注意:我已经访问过这个 post但这并不能解决我的问题

预先感谢您的帮助。

最佳答案

尝试使用consumes代替 header

@RequestMapping(value = "/insert",method = RequestMethod.POST, consumes="application/x-www-form-urlencoded","Content-Type = application/x-www-form-urlencoded"},consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)

关于java - @RequestMapping(headers)、@RequestBody 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47955862/

相关文章:

java - Spring boot中将实体从服务传输到前端的最合适的方式

java - 如何解决[类型删除],不建议在搜索请求中指定类型。“](elasticsearch 7.4.0,spring boot 2.2.6)?

java - 如何检查两个日期是否在一个时间段(间隔)之间?

java - 抑制 Java 中不推荐使用的导入警告

java.lang.IllegalArgumentException : Cannot convert Model. User[ usrId=1 ] 将类 Model.User 键入类 Model.User

javascript - Axios 正在阻止执行其他方法

node.js - 我的 NodeJS 服务器在 6 个 Axios post 请求后崩溃

java - 将输入字符串与数据库中的字符串进行比较

spring-boot - 无法使用gradle和sts生成JPA元模型casse

javascript - 链接 axios 请求