使用Ajax将json对象传递给Spring mvc控制器

时间:2022-12-01 15:58:31

I am trying to pass simple JSON object to Spring MVC controller and getting error as “NetworkError: 415 Unsupported Media Type - https://my_url.html

我试图将简单的JSON对象传递给Spring MVC控制器,并将错误称为“NetworkError:415 Unsupported Media Type - https://my_url.html”

I am using Spring 3.2.10, jquery 1.6 and com.googlecode.json-simple 1.1.1 libraries. Follow I post my code,

我使用的是Spring 3.2.10,jquery 1.6和com.googlecode.json-simple 1.1.1库。关注我发布我的代码,

JSP

function myTestFunction(year){
        $.ajax({
            type: "POST",                
            url: "my_url.html",
            data: "{\"testName\": \"MyName\"}",
            contentType: "application/json",
            mimeType: "application/json",
            success: function(response){
                console.log("SUCCESS: " + response);
            },
            error: function (e) {
                console.log("Error " + e);
            }
        });

Controller class

 @RequestMapping(value = "/my_url.html", method = RequestMethod.POST)
    public void myTestMethod(@RequestBody MyInfo myInfo, HttpServletRequest request, HttpServletResponse response) throws Exception{
// my code
 }

MyInfo class

    public class MyInfo {    
        private String testName;

        public MyInfo () {
        }   

        public MyInfo (String testName) {
            this.testName = testName;
        }

        public String getTestName() {
            return testName;
        }

        public void setTestName(String testName) {
            this.testName = testName;
        }
}

I have tried with several options by adding dataType: 'json' and sending object using JSON.stringify But didn't work for me. And also I already put the “<mvc:annotation-driven />” tag in my spring configuration file. What am I miss or doing incorrectly ?

我尝试了几个选项,添加了dataType:'json'并使用JSON.stringify发送对象但是对我来说不起作用。而且我已经在我的spring配置文件中添加了“ ”标记。我错过了什么或做错了什么?

2 个解决方案

#1


1  

Your request and mapping are OK.

您的请求和映射都可以。

The error you mentioned can occur when a converter attempts to convert the HTTP request to JAVA object. You mentioned that you are using a json-simple library. Spring MVC expects jackson libraries on the classpath, so this can very well be your issue.

当转换器尝试将HTTP请求转换为JAVA对象时,您提到的错误可能会发生。您提到您使用的是json简单库。 Spring MVC期望在类路径上使用jackson库,所以这很可能是你的问题。

Try adding jackson libraries to your project. If using maven, for spring 3.2 the following should be a proper dependency, if you're using a different build system, simply download from maven repository, but also check for the transitive dependencies (you'll notice them listed inside the maven file in the jar archive)

尝试将jackson库添加到项目中。如果使用maven,对于spring 3.2,以下应该是一个正确的依赖,如果你使用不同的构建系统,只需从maven存储库下载,还要检查传递依赖(你会注意到它们列在maven文件中) jar档案)

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

#2


0  

Try using JSON.stringify. The ajax call will look like

尝试使用JSON.stringify。 ajax调用看起来像

function myTestFunction(year){

    $.ajax({
        type: "POST",                
        url: "my_url.html",
        data: JSON.stringify({"testName": "MyName"}),
        contentType: "application/json",
        mimeType: "application/json",
        success: function(response){
            console.log("SUCCESS: " + response);
        },
        error: function (e) {
            console.log("Error " + e);
        }
    });

#1


1  

Your request and mapping are OK.

您的请求和映射都可以。

The error you mentioned can occur when a converter attempts to convert the HTTP request to JAVA object. You mentioned that you are using a json-simple library. Spring MVC expects jackson libraries on the classpath, so this can very well be your issue.

当转换器尝试将HTTP请求转换为JAVA对象时,您提到的错误可能会发生。您提到您使用的是json简单库。 Spring MVC期望在类路径上使用jackson库,所以这很可能是你的问题。

Try adding jackson libraries to your project. If using maven, for spring 3.2 the following should be a proper dependency, if you're using a different build system, simply download from maven repository, but also check for the transitive dependencies (you'll notice them listed inside the maven file in the jar archive)

尝试将jackson库添加到项目中。如果使用maven,对于spring 3.2,以下应该是一个正确的依赖,如果你使用不同的构建系统,只需从maven存储库下载,还要检查传递依赖(你会注意到它们列在maven文件中) jar档案)

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

#2


0  

Try using JSON.stringify. The ajax call will look like

尝试使用JSON.stringify。 ajax调用看起来像

function myTestFunction(year){

    $.ajax({
        type: "POST",                
        url: "my_url.html",
        data: JSON.stringify({"testName": "MyName"}),
        contentType: "application/json",
        mimeType: "application/json",
        success: function(response){
            console.log("SUCCESS: " + response);
        },
        error: function (e) {
            console.log("Error " + e);
        }
    });