【问题标题】:org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supportedorg.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application/json;charset=UTF-8'
【发布时间】:2015-11-01 20:55:03
【问题描述】:

我是 Spring World 的新手,甚至编写了我的第一个 Jquery。尝试使用 Ajax 调用从 JSP 页面获取 JSON 数据到控制器。但我收到错误 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported。请帮忙。

依赖关系:

<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;
    }

}

【问题讨论】:

    标签: java jquery json spring-mvc


    【解决方案1】:

    试试

    @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" />
    

    【讨论】:

    • 也许你不在 web.xml 中:CharacterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilter-class> encodingutf-8
    【解决方案2】:

    在 ajax 中删除这些行,然后重试: contentType : '应用程序/json', mimeType : '应用程序/json',

    【讨论】:

    • 感谢您的帮助.. 但它仍然无法正常工作。我不知道我在哪里犯错。修改后..它给出错误:org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported
    【解决方案3】:

    有趣的是,我刚刚遇到了类似的问题。我不确定你是否解决了你的问题。我的是一个简单的遗漏,导致返回相同的错误消息。确保您已启用 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>
    

    【讨论】:

      猜你喜欢
      • 2015-03-26
      • 2018-07-24
      • 1970-01-01
      • 2014-09-18
      • 2016-12-07
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2019-12-16
      相关资源
      最近更新 更多