spring-roo commands:
mkdir myrestful_resource
cd myrestful_resource
roo
project --topLevelPackage com.my.web.my.restful.resources
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity --class ~.domain.Resource1
field string --fieldName myfirstfield
field string --fieldName mysecondfield
controller all --package ~.controllers
osgi start --url http://spring-roo-addons.googlecode.com/files/net.tzolov.httprepresentations.roo.addon-1.1.7.jar
http resource representation setup
http resource add oxm --class ~.domain.Resource1
exit
json/html conversion works out of the box without modification however you need to annotate the model objects you want converted to xml by jaxb with @XmlRootElement so it knows what you want returned.
vi src/main/java/com/my/web/my/restful/resources/domain/Resource1.java
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVGy16EqvyZrfn6SrgXKXmQMCn8L_qc0W-d_cqkuvVCCZxvJ3Vr1vL0kC0pQxI67R9HsCao3iuAcmeusdJj9LsFrto1AvtSBGFAklQcnaTfPsWMY6zrIFEUqdJqI3yc5W4MCjWBS91KZE/s400/Screen+shot+2011-03-29+at+6.45.35+PM.png)
mvn tomcat:run
open up http://localhost:8080/resources in your browser and create some data on the auto-generated controllers.
access it via your browser or curl by simply appending .json .xml extensions to the url.
curl http://localhost:8080/resources/resource1s/1.json
curl http://localhost:8080/resources/resource1s/1.xml
curl http://localhost:8080/resources/resource1s/1.html
Pow!
also check out this springsource post about hitting json rest based services with curl.
http://www.springsource.org/roo/guide?w=base-json
if all you need is json, you could also simply issue the "json all" spring roo command.
ReplyDeleteThen access the service with the appropriate header like so.
curl -i -H Accept:application/json http://localhost:8080/resources/resource1s/1
also works for HTTP POST
ReplyDeletecurl -i -H "Accept:application/json" -H "Content-type: application/json" -X POST -d '{your json here}' http://localhost:8080/resources/resource1s