0

I have following json request on view side

index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" > $(document).ready(function(){ sendAjax(); }); function sendAjax() { $.ajax({ url: "/shop/main/order/model", type: 'POST', dataType: 'json', data: " { \"totalPrice\": 10, \"custumerName\":\"vasiya\", \"ItemsList\": [ { \"id\": 1,\"name\":\"qqq\", \"price\": 10, \"count\": 2 } ] }", contentType: 'application/json', mimeType: 'application/json', success: function(data) { alert(data.totalPrice + " " + data.custumerName); }, error:function(data,status,er) { alert("error: "+data+" status: "+status+" er:"+er); } }); } </script> </body> </html> 

controller looks like this

 @Controller @RequestMapping("/order") public class OrderController { @RequestMapping(value="/model", method = RequestMethod.POST) public @ResponseBody OrderModel post(@RequestBody final OrderModel model) { System.out.println(model.getItemsList()); System.out.println(model.getTotalPrice()); return model; } } } 

OrderModel.java

public class OrderModel implements Serializable { private static final long serialVersionUID = 1L; String custumerName; int totalPrice; Items[] itemsList; public void setTotalPrice(int totalPrice) { this.totalPrice = totalPrice; } public void setItemsList(Items[] itemsList) { this.itemsList = itemsList; } public int getTotalPrice() { return totalPrice; } public Items[] getItemsList() { return itemsList; } public void setCustumerName(String custumerName) { this.custumerName = custumerName; } public String getCustumerName() { return custumerName; } public OrderModel(String custumerName, int totalPrice, Items[] itemsList) { this.custumerName = custumerName; this.totalPrice = totalPrice; this.itemsList = itemsList; } } 

Items.java is a simple POJO class which is designed for mapping hibernate entities and contains String and long types fields only.

So when I'm trying to access index.html it doesn't send back any respone. In browser's console i'm getting the follow:

POST http://localhost:8080/shop/main/order/model 415 ()

HTTP Status 415 -

type Status report

message

description: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

5
  • at a first glance, I find a little strange those \" in the request body. try this data: { totalPrice: 10, custumerName:'vasiya', ItemsList: [ {id: 1,name:'qqq', price: 10, count: 2 } ] }, Commented May 13, 2016 at 13:46
  • nope, problem remains Commented May 13, 2016 at 14:42
  • let me point you here. Maybe it's the missing consumes="application/json" in the@RequestMapping Commented May 13, 2016 at 14:53
  • Yes, I have already noticed that, but it still doesn't work Commented May 13, 2016 at 15:00
  • I tried to imitate the same request via postman and got the same error, so it seems like it is the server side problem Commented May 13, 2016 at 15:17

2 Answers 2

0

2 Things I figured out from your code. 1. You need a messageConverter in your application.

To add message converter you need to add these 2 dependencies in your pom. Check the correct version which is applicable for you.

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> 

2. Add below bean in your dispature-servlet.xml or whatever mvc configuration file you are using.

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </list> </property> </bean> 

After this you will not going to get 415 as your application will find correct message converter.

  1. Your OrderModel entity is missing default constructor which will error out once you will do above 2 steps. so add a default constructor.

    public OrderModel() { } 
Sign up to request clarification or add additional context in comments.

3 Comments

You goddamn right! Thank a lot.But it works fine without additional bean definition. Is it really necessary? I had already jackson-mapper-asl in pom, but no jackson-databind, and default constructor.-It seems like those two was the main problem.
Can you send me your dispatcher-servlet.xml? As I tried without message converter and was getting 415. So I added it.
I think I was missing <mvc:annotation-driven /> in my context configuration file. After adding it. we don't need to define that message converter bean.
0

You are sending ItemsList but in your class it is itemsList.

1 Comment

oh thanks what an embarrassing mistake, but it's still the same result

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.