首页 > 解决方案 > 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>

标签: javamodel-view-controller

解决方案


尝试在你的控制器中做注释的下一个方法:

@RequestMapping(value="/register.do",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)

推荐阅读