- This topic has 11 replies, 3 voices, and was last updated 13 years, 4 months ago by jayperkins.
-
AuthorPosts
-
raj82rajMemberHi,
I am using myeclipse for spring 9.0 and i have generated Spring MVC application using Spring Scaffolding method.
I have generated MVC application using Customer Table as explained in the Tutorial of Scaffolding. I can test the application successfully from my web browser. [i.e see list, add customer etc.. properly]
While generating application, i have selected option Generate Restful Web services. My problem is i am not able to test the generated web Service. I could see CustomerRestController.java file generated so it seems the generation was proper. I tried following option to test the web service.
[1] In REST webservices explorer, typed http://localhost:8080/SpringDemo/application.wadl but says
Error: Cannot access WADL
Please make sure the WADL file is accessible at the above URL.[2] In browser typed http://localhost:8080/SpringDemo/Customer/ but no success
[3] Tried by Right clicking on Project to get option “Try with REST Webservice explorer” but that option doesn’t come up.
If any one can help me on how to test the REST webservice generated using Scaffolding, that would be great help.
Thanks,
Raj
raj82rajMemberHi,
Any help from Myeclipse support team or any forum member would be greatly appreciated as
i am stuck here.Thanks,
Raj
jayperkinsMemberHi Raj,
When you select the “Generate Restful Web services” in the crud scaffolding wizard, the code that is generated uses spring annotations for the restful web support. The spring support, however, is not an implementation of JAX-RS. In addition, no wadl is generated which the web services explorer requires in order to test a restful web service.
See the following link for more info on spring restful web service support:
http://www.ibm.com/developerworks/webservices/library/wa-spring3webserv/index.htmlIt also contains some resources for testing the restful web services. I use the firefox addon RestClient avaiable here:
https://addons.mozilla.org/en-us/firefox/addon/restclient/You do have to add two request headers explicitly when using this tool:
Accept = application/json
Content-Type = application/jsonLet me know if you still have problems.
Thanks,
Jay
raj82rajMemberHi Jay,
Thanks for your reply and providing the link of spring restful web service support. I went through the link and could better understand the REST support of spring.
I am experimenting more with Spring Scaffolding. I have now generated CRUD app for customer table using scaffolding with both the option selected for REST. i.e. i have selected option [1] Generate Restful Web services [2] Generate JSON Support.
As a result, i could successfully generate REST web service for Customer which supports JSON. i.e. When i try http://localhost:8080/SpringDemo/Customer/ , i m getting list of customers in proper JSON format and when i type http://localhost:8080/SpringDemo/Customer/112 , i am getting proper json for customer id 112. I tested other REST methods like POST, PUT, DELETE and every thing works proper. [ I am using REST Client available at http://code.google.com/p/rest-client/ because I could not open the link of REST client provided by you. ]So far so good. However, for my Project, i want MVC application along with REST webservices which supports XML too. The REST service generated above supports JSON properly but NOT XML.[i might be wrong. But i don’t know to how to test XML support] For my project, i want XML too along with JSON.
Am i suppose to do any changes in myeclipse Scaffold generated application to support both XML & JSON ? OR i just need to test with proper URL for XML? If i am suppose to change any configuration, please provide code for same. If i am not testing REST XML support properly, please guide how can i get XML.
Please let me know if you need additional information.
Thanks again for all your kind help and support.
Thanks,
Raj
neydeMember@raj82raj wrote:
Am i suppose to do any changes in myeclipse Scaffold generated application to support both XML & JSON ? OR i just need to test with proper URL for XML? If i am suppose to change any configuration, please provide code for same. If i am not testing REST XML support properly, please guide how can i get XML.
The Spring 3.0 code generation functions in ME4S generate according the MVC configuration simplifications introduced by Spring in Spring 3.0.
Here’s a link to there blog post:
http://blog.springsource.com/2009/12/21/mvc-simplifications-in-spring-3-0/According to the blog post, there is “Support for reading and writing XML, if JAXB is on the classpath“. Have you tried that?
raj82rajMemberHello Neyde,
Thanks for your response. Thanks also for sharing link. I went through it and found interesting.
As suggested by you and as indicated in the post, i decided to try same by scaffold new application. I selected option “Generate Restful Web services” ONLY. [ I didn’t select option
Generate JSON Support to experiment with XML only ] while scaffolding and generated new application.I added two jars namely jaxb-api-2.1.jar & jaxb-impl-2.1.10.jar and tried to access REST web service by hitting URL http://localhost:8080/xmltest/StoreMaster/ but no success. The browser says “The requested resource (/xmltest/WEB-INF/pages/StoreMaster) is not available.”. I then added following jars but no success
activation-1.1.jar , stax-api-1.0-2.jar , jersey-server-1.0.3.1.jar , jersey-core-1.0.3.1.jar
, jsr311-api-1.0.jar , asm-3.1.jarPlease note that my MVC application web interface is working fine.
I added JAXB jars in my existing application which produces JSON Properly but that didn’t make any difference. I can get JSON but not XML.
Please help me as i am stuck here.
Thanks,
Raj
raj82rajMemberHello Neyde,
This is just to clarify that when i say “i added the jars” i meant “i added jar in the classpath”. I have checked it properly in my eclipse classpath files that the added jars have been properly placed in classpath.
Thanks,
Raj
neydeMemberThe following blog post has some discussion related to XML support in Spring REST.
http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
Perhaps it’s not as easy as adding JAXB libraries to classpath.
jayperkinsMemberYou may want to post this question on the spring forums, but this is what I have found. It may or may not be how Spring prefers that you to do it. It seems like the integration between different message types (json, xml) is not completely seemless, at least out of the box.
The spring class that handles this is:
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverterIf you look at the source of this class, you will see that it only writes a response if the class to return is annotated with @XmlRootElement.
So the listCustomers() method does not meet this requirement so no response is generated because the list classes don’t have jaxb annotations.
In order to get a list of customers to return, you will need to create a list wrapper class annotated with @XmlRootElement. Here is an example for a list of customers:
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name=”customers”)
public class Customers {
private List<Customer> list;public Customers(){};
public Customers(List<Customer> customers){this.list = customers;}public List<Customer> getList() {
return list;
}public void setList(List<Customer> list) {
this.list = list;
}
}In addition, I had to add a method to the CustomerRestController to return the Customers class, instead of the List<Customer> list.
@RequestMapping(value = “/Customerxml”, method = RequestMethod.GET)
@ResponseBody
public Customers getCustomers() {
return new Customers(new java.util.ArrayList<Customer>(customerService.loadCustomers()));
}So you would have to access the /Customerxml url instead of the /Customer url.
The other rest methods work fine, if you add the @XmlRootElement annotation to the Customer.java class.
Of course, you may be able to create your own version of the Jaxb2RootElementHttpMessageConverter that handles List classes.
Hope this helps.
Jay
raj82rajMemberHi Jay,
Thanks for the detailed explanation. I will try it and see if it works on not.
I appreciate all the kind help and support you are providing.
Thanks,
Raj
raj82rajMemberHi Jay,
Thanks a lot for your detailed explanation. It worked perfect. I can now generate XML properly by following the steps suggested by you. Thank you. I am glad 🙂
I can also generate JSON if i remove @XmlRootElement. element.
I have a question though? Is it possible to have both JSON & XML enabled and client run time gets desired format based on format requested ? I mean if client request
http://myserver/myapp/Customer.xml then he should get XML output and if request is for http://myserver/myapp/Customer.json, then he should get JSON. I was reading some where that i am suppose to configure “ContentNegotiatingViewResolver “. I tried but could not make out where and how should i configure “ContentNegotiatingViewResolver” in myeclipse scaffold generated application.
Once again thank you very much for all your kind help and support.
Regards,
Raj
jayperkinsMemberCurrently, the format of what is returned is controlled by the accept http header. Just change from application/json to application/xml to get a different return format.
Does this answer your question?
-
AuthorPosts