开发者

POST curl request returns syantactic error on Spring tc server

I'm new to Spring, STS, MVC, and web development.

I have a working Spring REST-based web application. I also have a java client which works. I followed these 2 tutorials:

  1. RESTful webservices with Spring
  2. Get started with Spring MVC

What I was able to do is use a java client within the same project and use the RestTemplate postForLocation method. It works! However, now I would like the client to be an iPhone and I don't know how to do that. In the java client, the RestTemplate did a post using the com.project.Transaction class.

Please correct me if I am wrong here. In the XML file, the restTemplate "messageConverter" attribute gets set to jaxbMarshaller which is a Jaxb2Marshaller class with "Transaction" as one of the cla开发者_开发技巧sses bounded. I don't understand the details of it but I am assuming the RestTemplate takes the Transaction object and marshalls it into XML which gets sent to the server as a POST request.

First question: Is there any way I can see what the marshalled object (ie: the output) looks like? I'm using STS and please be specific as I am new. For example, maybe the data sent is like <xml ...?

Second question: I am trying to use curl to make a similar POST request as the java client. This is my curl request:

curl -X POST -H 'Accept:application/xml' -H 'Content-Type: application/xml' http://localhost:8080/BarcodePayment/transactions/ --data "<?xml version="1.0" encoding="UTF-8"?><transaction><amount>3.1</amount><id>5</id><paid>true</id></transaction>"

When I do that, I receive a STATUS 400: syntactically error message.

Third queston: I would love to be able to understand the details a little bit better. I looked into the source code for RestTemplate from here. In it, the postForLocation method uses HttpEntityRequestCallback which I cannot find any info in Google. I found HttpHeaders in java API doc. However, in RestTemplate, they call getLocation() method which does not exist in the Java API doc under javax.ws.rs.core -> HttpHeaders. How can I understand this stuff better?

I know it's a lot of question. Thanks for the help! Let me know if you need more code snippets and I'll be happy to provide!

Details: TransactionController

    @RequestMapping(method = RequestMethod.POST)
    public View addTransaction(@RequestBody Transaction transaction)
    {
        transactionService.saveTransaction(transaction);
        return new RedirectView("/transactionsView/"+ transaction.getId());
    }


Your xml is incorrect:

<transaction><amount>3.1</amount><id>5</id><paid>true</id></transaction>
                                                      ^^^ should be /paid


It turns out I have to use single quotes for the data. After --data "< xml .... ", I change it to ' < xml ... ' and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜