gpt4 book ai didi

java - HTTP 状态 415 - 使用 Ajax 将 Json 数据发布到 Spring MVC 4 还使用了 Spring Security

转载 作者:行者123 更新时间:2023-12-02 02:51:38 25 4
gpt4 key购买 nike

我正在尝试将数据从ajax发布到Spring Controller ,但无法将数据绑定(bind)到@ModelAttribute。我使用的是Spring版本4.3.3。我一直在尝试不同的方法,最终导致错误 400,405,406,415。我给出了下面的代码。请为我提供帮助,因为我坚持这个问题。

Spring Controller

@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:

^

因为我发送的数据没有与 Controller 中指定的@ModelAttribute绑定(bind)。

最佳答案

由于传入数据采用 JSON 格式,因此您无法使用 @ModelAttribute 绑定(bind)到 Java 对象。您必须先转换这些数据,然后再将它们绑定(bind)到您的模型。 Spring 提供了一种简单的方法来做到这一点,即使用 @RequestBody 注解。

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

关于java - HTTP 状态 415 - 使用 Ajax 将 Json 数据发布到 Spring MVC 4 还使用了 Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com