0

I have a request like that:

 let jsonData= {};
 jsonData["className"]= className;
 jsonData["models"]= arr;
 let endPoint= "/classses?classAndModel=" + encodeURIComponent(JSON.stringfy(jsonData));
return $.ajax({
 url: host + endPoint,
 data: data,
 cache: false,
 contentType: false,
 processData: false,
 method: "POST"
 });

I want to convert that json to java object.I tried this one

My rest service is: 
@PostMapping(value=/classes",consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> addClassAndModelMapping(ClassAndModels classAndModels){
}
public class ClassAndModels {
 ClassAndModelResult classAndModel;
 ...getter and setter...
 }
public ClassAndModelResult {
 String className;
 List<String> models;
 ...getter and setters...
 }

I get 400 error.If I change that line ClassAndModelResult classAndModel to String classAndResult.I get response but I want Object type.Do you have any idea?

asked Apr 24, 2019 at 15:01
3
  • 1
    You need to provide more detail. What does the JSON you are submitting look like? Have you tried creating an instance of ClassAndModels and serialising it to JSON to see how it compares to the JSON you are passing in? Commented Apr 24, 2019 at 15:27
  • You are using @PostMapping annotations. Looks like you are running a Spring (Boot) application, is this correct? Commented Apr 24, 2019 at 15:43
  • @smichel yes it is true Commented Apr 24, 2019 at 15:43

2 Answers 2

1

The first part of code shows that you are sending data as a query string.

Take a look at https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

But considering the @PostMapping, you should send that data in the request body and do something like this on the server side.

@PostMapping("/classes")
public ResponseEntity<Void> addClassAndModelMapping(@RequestBody ClassAndModels classAndModels){
 //
}

As Phils says, you can add a GetMapping on your controller to see how your ClassAndModels its being serialized

Source: https://spring.io/guides/tutorials/bookmarks/

P.S. Sorry about my english, I'm not a native speaker.

answered Apr 24, 2019 at 16:10
Sign up to request clarification or add additional context in comments.

4 Comments

Yes.Data is being sending as query string.I will add @RequestBody annotation
Can I use query string and @RequestBody together?
@sneijder10 yes. Try something like public ResponseEntity<Void> addClassAndModelMapping(@RequestBody ClassAndModels classAndModels, @RequestParam(required = false) String testParam). This way you can send host:port/api/classes?testParam=XXXXXXX and the json on the request body
@sneijder10 on ajax part, try dataType: 'json', contentType: 'application/json', and also set data: jsonData
0

Please try to add @RequestParam annotation or better use classAndModel value as RequestBody similar to the below.And also correct the spelling mistake in the javascript url.

@PostMapping(value = "/classes")
 public ResponseEntity<Void> addClassAndModelMapping(@RequestBody ClassAndModels modal) {
}
answered Apr 24, 2019 at 15:47

4 Comments

Did you correct that spelling mistake mentioned in the answer?
Please use application/json in consumes
Please send that json data along with complete request
Also I tried this one @RequestMapping(value = "/classes", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.