Spring Restful web services xml example

This post is in continuation with web service tutorial (Part -10). In this tutorial,we are going to see Spring Restful web services xml example.

Web service Tutorial Content:

In previous post, we have created a very simple Spring Restful web services  which returns json. In this post, we will see Spring Restful web services which will return xml as example.

Here are steps to create a simple Spring Restful web services which will return xml.

1) Create a dynamic web project using maven in eclipse.

2) For XML support, we just need to make sure JAXB jar is available in classpath.

pom.xml will be as follows:
pom.xml

Spring application configuration:

3) Change web.xml as below:

4) create a xml file named springrest-servlet.xml in /WEB-INF/ folder.
Please change context:component-scan if you want to use different package for spring to search for controller.Please refer to spring mvc hello world example for more understanding.

Create bean class

5) Create a bean name “Country.java” in org.arpit.java2blog.bean.

We need to annotate bean class with @XmlRootElement and @XmlElement to support for xml. As you can see we have annotated Country class with
JAXB annotation but if you want to have support for list, we can not edit ArrayList class, so we can create another class called CountryList  and we can annotate with JAXB annotation in that class to support xml output.
CountryList.java

Create controller

6) Create a controller named “CountryController.java”

@PathVariable: Used to inject values from the URL into a method parameter.This way you inject id in getCountryById method .

We are not providing any view information in springrest-servlet.xml as we do in Spring MVC. If we need to directly get resource from controller, we need to return @ResponseBody as per Spring 3 but with Spring 4, we can use @RestController for that.
In spring 4.0, we can use @RestController which is combination of @Controller + @ResponseBody.

6) It ‘s time to do maven build.

Right click on project ->Run as ->Maven build
7) Provide goals as clean install (given below) and click on run

 

Run the application

8) Right click on project ->run as ->run on server
Select apache tomcat and click on finish

 

When you run the application, you might get this kind of warning

Please ignore above warning. When you start application, you have below URL if you have not provided start page:
http://localhost:8080/SpringRestfulWebServicesWithXMLExample/ 

As we have used DispatcherServlet in web.xml, this request goes to spring DispatcherServlet and it did not find corresponding mapping in controller , hence you get that warning.

9) Test your REST service under: “http://localhost:8080/SpringRestfulWebServicesWithXMLExample/countries”.

You will get following output:

Spring Rest XML list example

 

10) Now pass country id as a parameter to url.
“http://localhost:8080/SpringRestfulWebServicesWithXMLExample/country/2”.

 

click to begin
20KB .zip
We are done with Spring Restful web services xml example. If you are still facing any issue, please comment.

That’s all about Spring Restful web services xml example.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *