简体   繁体   English

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

[英]org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

I am new in the Spring World and writing even my first Jquery. 我是Spring World的新手,甚至写了我的第一个Jquery。 trying to get the JSON data from JSP page using Ajax call, to the controller. 尝试使用Ajax调用从JSP页面获取JSON数据到控制器。 But i am getting error org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported. 但是我收到错误org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application / json; charset = UTF-8'。 Please do help. 请帮忙。

Dependencies: 依存关系:

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

Model Class: UserInfo.java package com.shop.model; 模型类: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;
    }

}

try 尝试

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

or setting tomcat 或设置tomcat

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

删除ajax中的这些行,然后重试:contentType:“ application / json”,mimeType:“ application / json”,

Interestingly I just ran into a similar problem. 有趣的是,我遇到了类似的问题。 I'm not sure if you resolved your problem yet or not. 我不确定您是否已解决问题。 Mine was a simple omission that was causing the same error message to be returned. 我的遗漏是一个简单的遗漏,导致返回了相同的错误消息。 Make sure you have enabled the annotation config for Spring MVC. 确保已为Spring MVC启用注释配置。 Within your dispatcher-servlet.xml configuration file make sure you have included 在您的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.web.HttpMediaTypeNotSupportedException:内容类型&#39;application / xml; charset = UTF-8&#39;不支持 - org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/xml;charset=UTF-8' not supported 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型&#39;text/plain;charset=UTF-8&#39; - Error: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported 警告:已解决 [org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型 'application/json'] - WARNING: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported] org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型&#39;application / json&#39;(Ajax,Spring) - org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported(Ajax, Spring) org.springframework.web.reactive.function.UnsupportedMediaTypeException:不支持内容类型&#39;application/json;charset=UTF-8&#39; - org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json;charset=UTF-8' not supported org.springframework.web.reactive.function.UnsupportedMediaTypeException:bodyType 不支持内容类型“text/xml;charset=UTF-8” - org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'text/xml;charset=UTF-8' not supported for bodyType org.springframework.web.client.RestClientException:提取类型和内容类型的响应时出错 [application/json;charset=utf-8] - org.springframework.web.client.RestClientException: Error while extracting response for type and content type [application/json;charset=utf-8] 不支持的媒体类型,“例外”:“ org.springframework.web.HttpMediaTypeNotSupportedException”,“消息”:“内容” - Unsupported Media Type“,”exception“:”org.springframework.web.HttpMediaTypeNotSupportedException“,”message“:”Content HttpMediaTypeNotSupportedException:不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8' - HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 错误org.springframework.web.HttpMediaTypeNotSupportedException - Error org.springframework.web.HttpMediaTypeNotSupportedException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM