java - org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver resolveException
问题描述
我是java mvc的新手,我遇到了同样的错误
2020 年 1 月 27 日晚上 10:48:16 org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver resolveException 警告:已解决 [org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型“应用程序/json”]
我附上了我的文件夹结构和代码。你能帮我做进一步的
文件夹结构吗
package com.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.bean.RegistrationBean;
@Controller
@RequestMapping("/helloworld")
public class InitController {
@RequestMapping(value="/register.do",method = RequestMethod.POST)
public String hello(@RequestBody RegistrationBean register,HttpServletRequest request) {
System.out.println("controller test");
return "asd";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
登记
<script>
$(document).ready(function() {
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
$('#submit').click(function() {
debugger
var data = $("#registration").serializeObject();
var dataToPort = JSON.stringify(data);
$.ajax({
type : 'POST',
contentType: "application/json",
url : '../helloworld/register.do',
dataType : "json",
data : dataToPort,
cache : false,
success : function(result) {
debugger
}
});
});
});
mvc-调度程序-servlet
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
<context:component-scan base-package="com.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
解决方案
尝试在你的控制器中做注释的下一个方法:
@RequestMapping(value="/register.do",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
推荐阅读
- svg - 在谈论图形或 svg 时,什么是用户坐标系
- python - 你如何将浮点数四舍五入到我选择的小数位(python)?
- django - 在 ListView 中获取当前页码
- mysql - 我可以在删除所有旧行(相同的主行除外)之前向 MySQL 添加行吗?
- c# - 如何使用背景图像切换背景颜色
- python - 为什么 Exception 将 __str__ 代理到 args 上?
- redux - Redux-saga setContext 在其他 saga 中不可用
- javascript - Select2 Optgroup 结果集
- c++ - 使用提升段在可比较距离中失败,而线串工作
- c# - 从 jpeg 文件中提取颜色(无位图)