【问题标题】:HTTP Status 415 - Post Json data to Spring MVC 4 using Ajax Also used Spring SecurityHTTP 状态 415 - 使用 Ajax 将 Json 数据发布到 Spring MVC 4 也使用了 Spring Security
【发布时间】:2017-05-03 07:06:41
【问题描述】:

我正在尝试将数据从 ajax 发布到 Spring Controller,但无法将数据绑定到 @ModelAttribute。我使用的是 Spring 4.3.3 版。我一直在尝试不同的方法,最终出现错误 400,405,406,415。我给出了下面的代码。请帮我解决这个问题。

弹簧控制器

@ResponseBody 
@RequestMapping(value="{urls}/design", method=RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public String getDesign(@PathVariable("urls") String urls, @ModelAttribute JsonExample jsonExample ){
    System.out.println(jsonExample);
    return jsonExample.getName();
}

Ajax 帖子

var url = $("#crumblink").attr("href");
var token = $('#csrfToken').val();
var header = $('#csrfHeader').val();
var data = {name:"Abc"};
            $.ajax({                
                type : 'POST',
                contentType : "application/json",
                url : url + '/design',
                data: JSON.stringify(data),
                dataType : 'json',              
                beforeSend: function(xhr) { 
                    xhr.setRequestHeader(header, token); 
                }, 
                success : function(data) {
                    console.log("SUCCESS: ", data);
                },
                error : function(e) {
                    console.log("ERROR: ", e);
                }
            });

JsonExample 模型类

public class JsonExample {  
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "JsonExample [name=" + name + "]";
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>******</groupId>
    <artifactId>***</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j13</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>   
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.4</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4.1209</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>dcs</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warName>dcs</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

当我提交详细信息时,我得到的只是

XML Parsing Error: no element found Location: moz-nullprincipal:{2269913a-adbb-43e8-bf5a-d2d9586f35f7} Line Number 1, Column 1:

^

因为我发送的数据没有与控制器中指定的@ModelAttribute 绑定。

【问题讨论】:

    标签: java ajax spring maven spring-mvc


    【解决方案1】:

    由于您的传入数据是 JSON 格式,因此您不能使用 @ModelAttribute 绑定到 Java 对象。您必须先转换这些数据,然后才能将它们绑定到您的模型。 Spring 提供了一种简单的方法来做到这一点,使用@RequestBodyannotation。

    @RequestBody 隐式使用消息转换器来转换您的数据,如果您的类路径中有 JSON 数据绑定 jar,Spring 会自动为您配置此消息转换器。

    【讨论】:

    • 是的,我用过它,我得到HTTP Status 415 - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. 错误。
    • 看看这里覆盖默认数据解析器:stackoverflow.com/questions/41036377/…
    • 哦....那很快...在void configureMessageConverters 下添加converters.add(new MappingJackson2HttpMessageConverter()); 就可以了。为你 +1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2015-02-12
    • 2015-11-20
    • 2013-01-11
    • 2015-01-31
    相关资源
    最近更新 更多