您的位置:首页 > 编程语言 > Java开发

CXF+Spring搭建Restful接口服务

2017-05-13 10:25 459 查看
之前项目中用CXF+Spring可以搭建WebService服务,后面发现cxf可以支持Restful规范的接口。于是,在接下去的练手项目中,打算用cxf+spring来搭建一个Restful接口服务。

战斗前的准备

我是在官网下的cxf最新版本,已经包含了spring的jar包。所以,我先把一些明确不会用到的jar包去除后,其他的全部导入了。

配置web.xml文件

主要是配置spring和cxf。

<!-- spring  监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml,classpath*:com/*/*Context.xml,classpath*:com/*/*/*Context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<!--这边配置的url-pattern在后面访问的时候会用到-->
<url-pattern>/web/*</url-pattern>
</servlet-mapping>


添加spring配置文件

新建一个applicationContext.xml(这边名字需要能与web.xml中配置的监听匹配)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> 
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="userRestServiceBean" class="com.rest.UserRestServiceImpl" />
<!-- 这里的地址,客户端需要通过这个地址来访问WebService -->
<jaxrs:server id="restServiceContainer" address="/rest">
<jaxrs:serviceBeans>
<ref bean="userRestServiceBean" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
<entry key="en" value="en-gb" />
</jaxrs:languageMappings>
</jaxrs:server>
</beans>


编写接口

配置文件都写完后就可以开始编写接口了。当有请求过来时,不止可以返回参数,也可以直接返回一个对象,并把返回的数据设置成xml格式或者json格式。接下来,来看看如何编写接口。

1、新建一个UserBean类

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "UserInfo")
public class User implements Serializable {
private static final l
4000
ong serialVersionUID = 677484458789332877L;
private int id;
private String name;
private String email;
private String address;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}


2、创建一个接口IUserRestService

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(value = "/rest/")
public interface IUserRestService {
@GET
@Path("/user/{id}")
//如果是要返回json格式的数据则用 MediaType.APPLICATION_JSON,返回xml格式则用MediaType.APPLICATION_XML
@Produces({MediaType.APPLICATION_JSON })
public User getUser(@PathParam("id") int id);

@POST
@Path("/result")
public String result(String json);
}


3、实现上面的接口

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(value = "/user/")
public class UserRestServiceImpl implements IUserRestService {
@GET
@Path("/user/{id}")
@Produces({MediaType.APPLICATION_JSON })
public User getUser(@PathParam("id") int id) {
User user = new User();
user.setId(id);
user.setName("test_name");
return user;
}

@POST
@Path("/result")
public String result(String data){
return data+",success";
}
}


到这里就结束了。是的,就这么简单。只要运行起来,上面的几个接口就可以访问了,至于访问地址……

测试访问接口

在确保创建的rest服务在本地正常运行后,接下来就测测能不能访问到接口。首先,来看一下访问地址,开头肯定是http://localhost:8080/项目名称无误。接着,是在web.xml中配置的url-pattern,然后再拼接spring中配置的address(上面代码片段有注释的)。然后是实现类中的path和具体接口的path。

项目名我用的是restful,web.xml中配置的是web,而spring中配置的是rest,实现类中的path为user,第一个接口为user/id,所以,第一个接口为

//id为我们要传入的值 http://localhost:8080/restful/web/rest/user/user/id[/code] 
第二个接口为result

http://localhost:8080/restful/web/rest/user/result


测试结果

1、get请求,返回json格式的对象



2、post请求,返回字符串

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring cxf restful