Reputation: 25
I'm getting 400 Bad Request on Ajax request when trying to map my JSON to a Java object in spring MVC controller. I have checked most of the relevant questions in the topic but still couldn't make it work
Ajax call and JSON:
$.ajax({
type: "POST",
url: "vocabulary/createVocabulary",
contentType : 'application/json; charset=utf-8',
data: {"vocabularyName" : "a",
"vocabularyDescription" : "b"},
My controller:
@Service
@Controller
@RequestMapping(path="vocabulary")
public class VocabularyController {
@RequestMapping (path = "createVocabulary", method = RequestMethod.POST)
@ResponseBody String createVocabulary(@RequestBody VocabularyDTO vocabularyDTO){
return "Success";
}
}
My Java object:
public class VocabularyDTO {
private String vocabularyName;
private String vocabularyDescription;
public VocabularyDTO(){};
public String getVocabularyName() {
return vocabularyName;
}
public void setVocabularyName(String vocabularyName) {
this.vocabularyName = vocabularyName;
}
public String getVocabularyDescription() {
return vocabularyDescription;
}
public void setVocabularyDescription(String vocabularyDescription) {
this.vocabularyDescription = vocabularyDescription;
}
}
I use Spring 4.2.5 and Jackson:
...
def springVersion = "4.2.5.RELEASE"
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile group: "org.springframework", name: "spring-core", version: "$springVersion"
compile group: "org.springframework", name: "spring-beans", version: "$springVersion"
compile group: "org.springframework", name: "spring-context", version: "$springVersion"
compile group: "org.springframework", name: "spring-aop", version: "$springVersion"
compile group: "org.springframework", name: "spring-web", version: "$springVersion"
compile group: "org.springframework", name: "spring-webmvc", version: "$springVersion"
compile group: "org.springframework.data", name: "spring-data-jpa", version: "1.9.4.RELEASE"
compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.6.5"
compile group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.6.5"
compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.6.5"
...
}
The error I'm getting:
HTTP ERROR 400 Problem accessing /ux/vocabulary/createVocabulary. Reason: BAD_REQUEST
Additionally, if I remove the '@RequestBody'annotation from my controller I get the following server response:
Failed to instantiate [com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO.()
Does anyone have any idea what could be wrong? Thanks!
UPDATE: The option without '@RequestBody' annotation is actually working now (it was a silly mistake of mine why it didn't before) - i.e. not throwing error, however without the values being passed to the object.
I was hoping it would fix the annotation as well but I still get the 400 error that way.
Upvotes: 2
Views: 4635
Reputation: 2427
Your code is not working when using @RequestBody
because your ajax request is not sending a json object. You need to serialise the JavaScript object before sending it, using JSON.stringify(). And why are you using @Service
at your controller? Try with:
$.ajax({
type: "POST",
url: "vocabulary/createVocabulary",
contentType : 'application/json; charset=utf-8',
data: JSON.stringify({"vocabularyName" : "a",
"vocabularyDescription" : "b"}),
@Controller
@RequestMapping(path="vocabulary")
public class VocabularyController {
@RequestMapping (path = "createVocabulary", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE}
@ResponseBody String createVocabulary(@RequestBody VocabularyDTO vocabularyDTO){
return "Success";
}
}
Upvotes: 2