Я новичок в java mvc, я получаю ту же ошибку "27 января 2020 г., 22:48:16 org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver resolException ПРЕДУПРЕЖДЕНИЕ: Resolved [org.springframework. web.HttpMediaTypeNotSupportedException: тип содержимого «application / 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 -диспетчер-сервлет
<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>