2 qq 26907275 qq_26907275 于 2016.04.04 20:31 提问

SpringMVC使用注解解析json总是报415错误 20C

用HTML访问这个总是报415错,
这是Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
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"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--指明 controller 所在包,并扫描其中的注解-->
<context:component-scan base-package="com.ch.controller"/>

<!-- 静态资源(js、image等)的访问 -->
<mvc:default-servlet-handler/>

<!-- 开启注解 -->
<mvc:annotation-driven />

<!--ViewResolver 视图解析器-->
<!--用于支持Servlet、JSP视图解析-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<!-- 表示JPA Repository所在的包 -->

<!-- 链接到persistence.xml -->


<!-- 事务管理 -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/bookshop"></property>
    <property name="username" value="root"/>
    <property name="password" value="689571"/>
    <!-- 数据库连接池保持的最小连接数 -->
    <property name="minIdle" value="5"/>
    <!-- 数据库连接池保持的最大连接数 -->
    <property name="maxIdle" value="30"/>
    <!--
        当数据库连接因为某种原因断掉之后,再重新从连接池中拿另外一个连接时实际上这个连接可能
        已经无效,所以为了确保所拿到的连接全都有效需要在获取连接,返回连接以及连接空闲时进行
        有效性验证 下面3个设置为ture时进行验证,默认为false
     -->
    <!-- 取得连接时是否进行有效性验证 -->
    <property name="testOnBorrow" value="true"/>
    <!-- 返回连接时是否进行有效性验证 -->
    <property name="testOnReturn" value="true"/>
    <!-- 连接空闲时是否进行有效性验证 -->
    <property name="testWhileIdle" value="true"/>
</bean>
<!-- 设置Hibernate sessionFactory -->
<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="datasource"/>
    <property name="packagesToScan" value="com.ch.model"/>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.transaction.coordinator_class">jdbc</prop>
        </props>
    </property>
</bean>
<!-- 设置Spring事务 -->
<bean id="txManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--启动spring事务注解功能-->
<tx:annotation-driven transaction-manager="txManager"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter"/>
        </list>
    </property>
</bean>
<bean id="mappingJacksonHttpMessageConverter"
      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>application/json;charset=UTF-8</value>
            <value>text/html;charset=UTF-8</value>
        </list>
    </property>
</bean>

WEB配置文件
<?xml version="1.0" encoding="UTF-8"?>
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"
version="3.1">

SpringMVCDemo Web Application


mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
1


mvc-dispatcher
/


encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true



encodingFilter
/*

所有的包和依赖
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.ch123
myspringtest
war
1.0-SNAPSHOT
bookahop Maven Webapp
http://maven.apache.org

4.2.0.RELEASE
1.2.0.RELEASE


<!-- hibernate 4.0 -->

org.springframework
spring-core
${spring.version}


commons-dbcp
commons-dbcp
1.4


com.fasterxml.jackson.core
jackson-annotations
2.1.0


com.fasterxml.jackson.core
jackson-core
2.5.3


com.fasterxml.jackson.core
jackson-databind
2.1.0


org.springframework
spring-web
${spring.version}

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
</dependency>

<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${spring.version}</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>${spring-data.version}</version>
</dependency>

<dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <version>1.0.0.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>4.2.2.Final</version>
</dependency>
<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
</dependency>



bookshop


控制器
package com.ch.controller;

import com.ch.bean.OBeanBase;
import com.ch.bean.userinfo.userloginIbean;
import com.ch.model.UsersEntity;
import com.ch.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**

  • Created by 57183 on 2016/3/29.
    */
    @Controller
    public class MainController {
    @RequestMapping(value = "/my", method = RequestMethod.POST)
    public String index(@RequestParam String userID,String password) {
    UsersEntity use=new UsersEntity(userID,password);
    UserService us = new UserService();
    if (us.checklogin(use)) {
    return "index";
    } else {
    return "err";
    }
    }

    HTML代码:
    <html>
    


    Spring Hello World!



    用户名
    密码


3个回答

CSDNXIAOD
CSDNXIAOD   2016.04.04 20:42

SpringMVC使用注解@ResponseBody返回json中文乱码
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

devmiao
devmiao   Ds   Rxr 2016.04.04 23:41
wenxuechaozhe
wenxuechaozhe   2016.04.05 09:03
 <bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json" />
                <entry key="html" value="text/html" />
                <entry key="file" value="application/octet-stream" />
                <entry key="apk" value="application/octet-stream"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean
                    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/pages/" />
                    <property name="suffix" value=".jsp" />
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
            </list>
        </property>
    </bean>

没有添加json解析吧

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!