org.springframework.web.HttpMediaTypeNotSupportedException:内容类型' application / json; charset = UTF-8'不支持

时间:2015-08-10 03:16:59

标签: java jquery json spring-mvc

我是春天世界的新人,甚至还写了我的第一个Jquery。尝试使用Ajax调用从JSP页面获取JSON数据到控制器。但我收到错误org.springframework.web.HttpMediaTypeNotSupportedException:内容类型' application / json; charset = UTF-8'不支持。请帮忙。

依赖关系:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>${jackson-asl}</version>
</dependency>

register.jsp

<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script type="text/javascript">
    alert("submit");
    $(document).ready(function() {
        alert("documet");
        $('#idForm').submit(function(e) {
            alert("idForm");
            e.preventDefault();
            alert("INSIDE jquery");
            var fName = $('#fName').val();
            var mName = $('#mName').val();
            var lName = $('#lName').val();
            var uName = $('#uName').val();
            var email = $('#email').val();
            var password = $('#password').val();
            var mobile = $('#mobile').val();
            var gender = $('#gender').val();
            var user = {
                "fname" : fName,
                "mName" : mName,
                "lName" : lName,
                "uName" : uName,
                "email" : email,
                "password" : password,
                "mobile" : mobile,
                "gender" : gender
            };

            alert(user);

            $.ajax({
                url : $('#idForm').action,
                dataType : 'json',
                type : 'POST',
                async : false,
                data : JSON.stringify(user), //Stringified Json Object
                contentType : 'application/json',
                mimeType : 'application/json',
                success : function(user) {
                    alert(user);
                }
            });
        });
    });
</script>

Controller.Java

@RequestMapping(value = "/register", method = RequestMethod.POST)
    public String registerPost(@RequestBody  UserInfo user) {
        LOG.info(user);
        return "login";
    }

模型类:UserInfo.java 包com.shop.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "userinfo")
public class UserInfo {

    private int userId;
    private String fName;
    private String mName;
    private String lName;
    private String uName;
    private String email;
    private String password;
    private String Gender;
    private Long mobile;

    public UserInfo() {

    }

    public UserInfo(int userId, String fName, String mName, String lName,
            String uName, String email, String password, String gender,
            Long mobile) {
        super();
        this.userId = userId;
        this.fName = fName;
        this.mName = mName;
        this.lName = lName;
        this.uName = uName;
        this.email = email;
        this.password = password;
        Gender = gender;
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return "UserInfo [userId=" + userId + ", fName=" + fName + ", mName="
                + mName + ", lName=" + lName + ", uName=" + uName + ", email="
                + email + ", password=" + password + ", Gender=" + Gender
                + ", mobile=" + mobile + "]";
    }

    @Id
    @GeneratedValue
    @Column(name = "userId", unique = true, nullable = false, length = 11)
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Column(name = "email", unique = true, nullable = false, length = 45)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "password", unique = true, nullable = false, length = 45)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "FirstName", nullable = false, length = 45)
    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    @Column(name = "middleName", nullable = false, length = 45)
    public String getmName() {
        return mName;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    @Column(name = "LastName", nullable = false, length = 45)
    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    @Column(name = "uName", unique = true, nullable = false, length = 45)
    public String getuName() {
        return uName;
    }

    public void setuName(String uName) {
        this.uName = uName;
    }

    @Column(name = "Gender", nullable = false, length = 45)
    public String getGender() {
        return Gender;
    }

    public void setGender(String gender) {
        Gender = gender;
    }

    @Column(name = "Mobile", nullable = false, length = 45)
    public Long getMobile() {
        return mobile;
    }

    public void setMobile(Long mobile) {
        this.mobile = mobile;
    }

}

3 个答案:

答案 0 :(得分:0)

@RequestMapping(value = "/register", method = RequestMethod.POST ,produces  = "text/plain;charset=UTF-8")

或设置tomcat

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" URIEncoding="UTF-8" />

答案 1 :(得分:0)

删除ajax中的这些行,然后重试:  contentType:&#39; application / json&#39;,                 mimeType:&#39; application / json&#39;,

答案 2 :(得分:0)

有趣的是,我遇到了类似的问题。我不确定你是否解决了你的问题。我的是一个简单的遗漏,导致返回相同的错误消息。确保已为Spring MVC启用了注释配置。在dispatcher-servlet.xml配置文件中,请确保已包含

<?xml version="1.0" encoding="UTF-8" standalone="no"?><beans xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   ...
   <mvc:annotation-driven/>
   ...
   </beans>