Spring3 and REST Integration(III)

本文通过实例演示了如何在 Spring 3 中使用 RESTful 风格进行数据交互,包括配置控制器来响应 GET 请求并返回 JSON 数据,以及如何利用 Fiddler2 和 HTTP Debugger 工具捕获浏览器与服务器之间的数据传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值