2

I'm doing a Spring MVC controller and I still get problem with POST operation. I've read many solutions on stackoverflow without to fix my problem.

My achievement at the moment :

  • I sent a GET request with an Id and return an Object converted to JSON successfully.
  • I failed to send a POST request with a JSON body, return = 415 UNSUPPORTED_MEDIA_TYPE

1) I added to my pom.xml the Jackson API : 1.8.5

2) My Spring configuration file: I added all necessary parts :

  • viewResolver
  • org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
  • MappingJacksonHttpMessageConverter
  • mvc:annotation-driven
  • scan my controllers

3) My model object is simple : an Account with Id, Name and an amount

@Document
public class Account implements Serializable {

    private static final long serialVersionUID = 9058933587701674803L;

    @Id
    private String id;
    private String name;
    private Double amount=0.0;

    // and all get and set methods 

4) and finally my simplified Controller class :

@Controller
public class AdminController {

    @RequestMapping(value="/account", method=RequestMethod.POST, 
             headers = {"content-type=application/json"})
    @ResponseStatus( HttpStatus.CREATED )
    public void addAccount(@RequestBody Account account){ 
        log.debug("account from json request " + account);
    }


    @RequestMapping(value="/account/{accountId}", method=RequestMethod.GET)
    @ResponseBody
    public Account getAccount(@PathVariable("accountId") long id){
        log.debug("account from json request " + id);
        return new Account();
    }
}

5) On client side I've just executed curl commands : The successfully GET command :

curl -i -GET -H 'Accept: application/json'  http://myhost:8080/compta/account/1

The POST command which failed:

curl -i -POST -H 'Accept: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account

Any ideas where I'm going wrong?

Ram
  • 3,092
  • 10
  • 40
  • 56
user1842947
  • 1,047
  • 1
  • 11
  • 16

2 Answers2

6

Well, "UNSUPPORTED_MEDIA_TYPE" should be a hint. Your curl command is actually sending:

Content-Type: application/x-www-form-urlencoded

Simply add explicit Content-Type header and you're good to go:

curl -v -i -POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • thank you for your answer, I get now another bad HTTP status 400 – user1842947 Jan 08 '13 at 19:59
  • 1
    @user1842947: this means you are sending incorrect JSON (*Bad request*) which Spring MVC can't parse. Please provide more details to your question or (better) mark this question as answered and open another one. – Tomasz Nurkiewicz Jan 08 '13 at 20:01
  • ok, thx, I try to add my controller class in my question for more details but I get Stack overflow error message about indenting code (4 spaces) I try to fix it and I'll publish it – user1842947 Jan 08 '13 at 20:07
  • @user1842947: wild guess: can you try: `amount:0.0` (without double quotes)? Also try "id":"1"` (**with** quotes). – Tomasz Nurkiewicz Jan 08 '13 at 20:21
  • I've tried without the amount field and I have the same return, and yes I've tried "id":"1" too ... sorry – user1842947 Jan 08 '13 at 20:25
  • 1
    Ok I've found my mistake : I didn't put get and set methods for the Id in ma Account class. thanks a lot, your first answer has helped me – user1842947 Jan 08 '13 at 20:33
4

Try this :

curl -i -POST -H "Accept: application/json" -H "Content-type: application/json" -d '{"id":1,"name":"test",amount:"0.0"}' http://myhost:8080/compta/account
Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
  • thx for your answer. the option -X return no result with my curl command. and Contet-type return another bad HTTP status : 400 – user1842947 Jan 08 '13 at 20:02