zoukankan      html  css  js  c++  java
  • SpringMVC接收json数据转对象中的一些问题(415错误的解决)

    在使用@requestBody方法获取传入的json字符串转化成对象的时候一直报415的错,主要是由于在springmvc.xml中没添加这部分:

    controller写法:

    package com.liang.testspring.controller;
    
    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 org.springframework.web.bind.annotation.ResponseBody;
    import com.liang.testspring.model.User;
    
    @Controller
    public class UserController {
    
        @ResponseBody
        @RequestMapping(method = RequestMethod.POST, value = "/login")
        public String login(@RequestBody User user) {
            System.out.println("a====a");
            System.out.println(user.getUserName());
            return "aaa";
        }
    
    }
     

    springMvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
            
            
            <!-- 配置自动扫描的包 -->
            <context:component-scan base-package="com.liang.testspring"></context:component-scan>
            
            <mvc:annotation-driven></mvc:annotation-driven>
            <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name = "prefix" value="/WEB-INF/views/"></property>
                <property name = "suffix" value = ".jsp"></property>
            </bean>
    </beans>

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
             <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 配置Spring mvc下的配置文件的位置和名称 -->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
      </servlet>
      
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
      
    </web-app>

    User 实体类:

    /**
     * 
     */
    package com.liang.testspring.model;
    
    /**
     * @author
     *
     */
    public class User {
    
        private int userId;
        private String userName;
        private String userPwd;
        public int getUserId() {
            return userId;
        }
        public void setUserId(int userId) {
            this.userId = userId;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getUserPwd() {
            return userPwd;
        }
        public void setUserPwd(String userPwd) {
            this.userPwd = userPwd;
        }
        
        
    }

    本文参考了如下内容:http://blog.csdn.net/u012556150/article/details/50574053

  • 相关阅读:
    [转] Chrome
    阿里安全潘多拉实验室首先完美越狱苹果iOS 11.2
    【阿里聚安全·安全周刊】阿里双11技术十二讲直播预约|AWS S3配置错误曝光NSA陆军机密文件
    卡巴斯基发布安全公告:2018年威胁预测
    【阿里聚安全·安全周刊】双十一背后的“霸下-七层流量清洗”系统| 大疆 VS “白帽子”,到底谁威胁了谁?
    分享一个白帽交流灵感的社区——先知技术安全社区
    WiFi网络WPA2 KRACK漏洞分析报告
    #云栖大会# 移动安全专场——APP渠道推广作弊攻防那些事儿(演讲速记)
    #云栖大会# 移动安全专场——APP加固新方向(演讲速记)
    Java安全编码:糟糕的在线建议和令人困惑的APIs
  • 原文地址:https://www.cnblogs.com/20000ding/p/6820451.html
Copyright © 2011-2022 走看看