Spring3 and REST Integration(III)
7. Try to watch the data between the server and client
First of all, I add some header to the PersonController.groovy file, so that I can use GET HTTP method to get the data via browers.
What I changed are here:
@RequestMapping(method = RequestMethod.GET, value = "/person/{id}", headers = "Accept=application/json, application/xml")
@RequestMapping(method = RequestMethod.GET, value = "/persons", headers = "Accept=application/json, application/xml")
ok, from this, we can get the data in chrome with these URLs:
http://localhost:8080/easyrestproxy/service/person/6
http://localhost:8080/easyrestproxy/service/persons
We can see the results are all in json format:
result for http://localhost:8080/easyrestproxy/service/person/6
{"name":"hahaTestJson","id":6,"sex":true,"age":1,"birthday":1296008693000}
result for http://localhost:8080/easyrestproxy/service/persons
{"amount":8,"persons":[{"name":"test","id":1,"sex":true,"age":1,"birthday":null},{"name":"testName","id":2,"sex":true,"age":1,"birthday":1295402858000},{"name":"testName","id":3,"sex":true,"age":1,"birthday":1295403234000},{"name":"testName","id":4,"sex":true,"age":1,"birthday":1295492666000},{"name":"testName","id":5,"sex":true,"age":1,"birthday":1295493176000},{"name":"hahaTestJson","id":6,"sex":true,"age":1,"birthday":1296008693000},{"name":"json","id":11,"sex":false,"age":1,"birthday":1296009839000},{"name":"fire in the hole","id":12,"sex":false,"age":1,"birthday":1296009839000}]}
8. We will try to grap the data
First tool, I tried fiddler2, it is free.
http://www.fiddler2.com/fiddler2/version.asp
but it only work fine when I use my browers. I do not know why, maybe I configured in wrong way.
Second tool, I tried httpdebugger, it is much more powerful.
http://www.httpdebugger.com/http/http_analyzer.html
It can grap the data, even I am running my junit test in Eclipse.
9. One more thing
groovy eclipse plugin URL: http://dist.springsource.org/milestone/GRECLIPSE/e3.5
And When I am writing the controller, I got this error at first:
No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either.
I google it and find a solution from this article:
http://www.objectpartners.com/2010/08/12/spring-pathvariable-head-slapper/
Thanks a lot. I change my method from
public @ResponseBody Person update(
@RequestBody Person p, @PathVariable String id) {
to
public @ResponseBody Person update(
@RequestBody Person p, @PathVariable("id") String id) {
That really works fine in my sample project.
references:
http://www.ibm.com/developerworks/web/library/wa-spring3webserv/index.html
http://www.ibm.com/developerworks/web/library/wa-restful/
7. Try to watch the data between the server and client
First of all, I add some header to the PersonController.groovy file, so that I can use GET HTTP method to get the data via browers.
What I changed are here:
@RequestMapping(method = RequestMethod.GET, value = "/person/{id}", headers = "Accept=application/json, application/xml")
@RequestMapping(method = RequestMethod.GET, value = "/persons", headers = "Accept=application/json, application/xml")
ok, from this, we can get the data in chrome with these URLs:
http://localhost:8080/easyrestproxy/service/person/6
http://localhost:8080/easyrestproxy/service/persons
We can see the results are all in json format:
result for http://localhost:8080/easyrestproxy/service/person/6
{"name":"hahaTestJson","id":6,"sex":true,"age":1,"birthday":1296008693000}
result for http://localhost:8080/easyrestproxy/service/persons
{"amount":8,"persons":[{"name":"test","id":1,"sex":true,"age":1,"birthday":null},{"name":"testName","id":2,"sex":true,"age":1,"birthday":1295402858000},{"name":"testName","id":3,"sex":true,"age":1,"birthday":1295403234000},{"name":"testName","id":4,"sex":true,"age":1,"birthday":1295492666000},{"name":"testName","id":5,"sex":true,"age":1,"birthday":1295493176000},{"name":"hahaTestJson","id":6,"sex":true,"age":1,"birthday":1296008693000},{"name":"json","id":11,"sex":false,"age":1,"birthday":1296009839000},{"name":"fire in the hole","id":12,"sex":false,"age":1,"birthday":1296009839000}]}
8. We will try to grap the data
First tool, I tried fiddler2, it is free.
http://www.fiddler2.com/fiddler2/version.asp
but it only work fine when I use my browers. I do not know why, maybe I configured in wrong way.
Second tool, I tried httpdebugger, it is much more powerful.
http://www.httpdebugger.com/http/http_analyzer.html
It can grap the data, even I am running my junit test in Eclipse.
9. One more thing
groovy eclipse plugin URL: http://dist.springsource.org/milestone/GRECLIPSE/e3.5
And When I am writing the controller, I got this error at first:
No parameter name specified for argument of type [java.lang.String], and no parameter name information found in class file either.
I google it and find a solution from this article:
http://www.objectpartners.com/2010/08/12/spring-pathvariable-head-slapper/
Thanks a lot. I change my method from
public @ResponseBody Person update(
@RequestBody Person p, @PathVariable String id) {
to
public @ResponseBody Person update(
@RequestBody Person p, @PathVariable("id") String id) {
That really works fine in my sample project.
references:
http://www.ibm.com/developerworks/web/library/wa-spring3webserv/index.html
http://www.ibm.com/developerworks/web/library/wa-restful/