I am stumped with a simple subject. I am trying to populate and send a custom java object as data for my controller using ajax. Would you have a pointer as to why it fails with a 400 error (Bad Request)?
Here is the custom object:
import org.springframework.stereotype.Component;
import lombok.Getter;
import lombok.Setter;
@Component
@Getter
@Setter
public class XpTableCriteria {
String globalSearch;
String columnsSearchCriteria;
}
And here is the script in my html page :
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id='result'>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
getTableData();
}
);
function getTableData(){
var xpTableCriteria = {globalSearch: "test", columnsSearchCriteria: "test2"};
var parameter = {'xpTableCriteria': xpTableCriteria};
$.ajax({
type : 'GET',
data : {parameter},
dataType: 'json',
url : 'all',
success : function(result) {
alert("OK");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
}
});
}
</script>
</body>
</html>
The controller itself does not seem to be relevant, as the bug occurs before entering it. Here it is included:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.domain.XpTableCriteria;
@RestController
public class Controller {
@GetMapping(value = "/all")
@ResponseBody
public XpTableCriteria getAll(@RequestParam XpTableCriteria xpTableCriteria) {
return null;
}
}
Here is the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ajaxobject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ajaxobject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Thank you very much in advance!