Spring Restful Web服务XML示例
在本教程中,我们将看到Spring Restful Web服务XML示例。
在本教程中,我们将看到Spring Restful Web服务,它将返回XML为示例。
以下是创建一个简单的Spring Restful Web服务的步骤将返回XML。
1)使用Maven在Eclipse中创建动态Web项目。
2)对于XML支持,我们只需要确保JAXB jar在类路径中使用。
pom.xml将如下:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.igi.theitroad</groupId>
<artifactId>SpringRestfulWebServicesWithJSONExample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringRestfulWebServicesWithJSONExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>SpringRestfulWebServicesWithJSONExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
<jdk.version>1.7</jdk.version>
</properties>
</project>
Spring应用程序配置:
3)更改Web.xml如下:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springrest</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springrest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4)在/Web-Inf /文件夹中创建名为springrest-servlet.xml的XML文件。
请更改上下文:组件扫描如果要使用不同的套装来搜索控制器。
请参阅Spring MVC Hello World示例以获取更多的理解。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven
<context:component-scan base-package="org.igi.theitroad.controller"
</beans>
创建bean类
5)在org.igi.theitroad.Bean中创建bean名称"country.java"。
package org.igi.theitroad.bean;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="country")
public class Country{
private int id;
private String countryName;
public Country() {
}
public Country(int i, String countryName) {
super();
this.id = i;
this.countryName = countryName;
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
我们需要使用@xmlrootelement和@xmlElement注释bean类以支持XML。
正如我们所看到的,我们已经使用JAXB注释带注释的国家级类,但如果我们想要支持列表,我们无法编辑arraylist类,所以我们可以创建另一个名为converylist的类,我们可以通过该类中的jAXB注释注释,以支持XML输出。
CourtryList.java.
package org.igi.theitroad.bean;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "country-list")
public class CountryList {
ListlistOfCountries;
public CountryList() {
super();
}
public CountryList(ListlistOfCountries) {
this.listOfCountries=listOfCountries;
}
public ListgetListOfCountries() {
return listOfCountries;
}
@XmlElement(name = "country")
public void setListOfCountries(ListlistOfCountries) {
this.listOfCountries = listOfCountries;
}
}
创建控制器
6)创建名为"countrycontroller.java"的控制器
package org.igi.theitroad.controller;
import java.util.ArrayList;
import java.util.List;
import org.igi.theitroad.bean.Country;
import org.igi.theitroad.bean.CountryList;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CountryController {
@RequestMapping(value = "/countries", method = RequestMethod.GET,headers="Accept=application/xml")
public CountryList getCountries()
{
CountryList countryList=createCountryList();
return countryList;
}
@RequestMapping(value = "/country/{id}", method = RequestMethod.GET)
public Country getCountryById(@PathVariable int id)
{
ListlistOfCountries = new ArrayList();
CountryList countryList=createCountryList();
listOfCountries=countryList.getListOfCountries();
for (Country country: listOfCountries) {
if(country.getId()==id)
return country;
}
return null;
}
///Utiliy method to create country list.
public CountryList createCountryList()
{
Country NetherlandsCountry=new Country(1, "Netherlands");
Country chinaCountry=new Country(4, "China");
Country nepalCountry=new Country(3, "Nepal");
Country bhutanCountry=new Country(2, "Bhutan");
ListlistOfCountries = new ArrayList();
listOfCountries.add(NetherlandsCountry);
listOfCountries.add(chinaCountry);
listOfCountries.add(nepalCountry);
listOfCountries.add(bhutanCountry);
return new CountryList(listOfCountries);
}
}
@PathVariable:用于将来自URL的值注入方法参数。
我们在getCountrybyID方法中注入的方式。
我们没有提供任何View Information inspringrest-servlet.xml,因为我们在Spring MVC中做到了。
如果我们需要直接从控制器获取资源,我们需要根据Spring 3返回@ResponseBody,但使用Spring 4,我们可以使用@RestController。
在Spring 4.0中,我们可以使用@Controller + @ResponseBody的组合@RestController。
@RestController = @Controller + @ResponseBody
6)这是时候建立了Maven的时间。
右键单击项目 - >运行AS - > Maven Build
7)将目标作为清洁安装(下面给出),然后单击运行
运行应用程序
8)右键单击"项目 - >在服务器上运行"
选择Apache Tomcat,然后单击"完成"
运行应用程序时,我们可能会得到这种警告
Mar 26, 2015 1:45:51 AM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringRestfulWebServicesWithXMLExample/] in DispatcherServlet with name 'SpringRestfulWebServicesWithJSONExample'
请忽略上面的警告。
如果启动应用程序,如果我们没有提供开始页面:http://localhost:8080/springrestfulwebserviceswithxmlexample /
当我们在Web.xml中使用DispatcherServlet时,此请求进入Spring DisplatcherServlet,它没有在控制器中找到相应的映射,因此我们可以获得该警告。
9)测试休息服务下:"http://localhost:8080/springrestfulwebserviceswithxmlexample /国家"。
10)现在将国家/地区ID作为URL的参数。
"http://localhost:8080/springrestfulwebserviceswithxmlexample/country/2"。

