xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" 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">
address="/userService">
F.启动服务器进行发布部署,在浏览器中输入http://localhost:8080/SpringCXF/services/userService?wsdl 看页面是否会出现xml文档,来判断服务是否配置成功
G:客户端配置
server.client 的配置
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: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"
<
import
resource
="classpath:META-INF/cxf/cxf.xml"/>
<
import
resource
="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<
import
resource
="classpath:META-INF/cxf/cxf-servlet.xml"/>
<
jaxws:client
id
="userWsClient"
serviceClass
="com.hoo.service.IComplexUserService"
address
="http://localhost:8080/
/SpringCXF/services/userService
"/>
beans
>
2.
RESTful Webservice
RESTful风格的
WebService
主张重用
HTTP
协议,面向资源编程(
ROA
)。扼要的说,
RESTful
风格
WebService
中,每一个
URL
即表示一个资源.
RESTful 简化了 web service 的设计,它不再需要 wsdl ,也不再需要 soap 协议,而是通过最简单的 http 协议传输数据 ( 包括 xml 或 json) 。既简化了设计,也减少了网络传输量(因为只传输代表数据的 xml 或 json ,没有额外的 xml 包装)
A.pom.xml配置,引入依赖的jar包
javax.ws.rs jsr311-api 1.1.1
org.apache.cxf cxf-rt-frontend-jaxrs 2.4.4
web.xml配置
Apache CXF Endpoint
cxf
cxf
org.apache.cxf.transport.servlet.CXFServlet
1
cxf
/services/*
B.restful webService 注解讲解
标签 |
用法和解释 |
@GET,@POST,@PUT,@DELETE |
该组标签将一个标注方法的 HTTP 请求类型 |
@PATH |
该标签可用于类或者方法,表示定位此 RESTful 资源的路径。 |
@Produces |
该标签标注资源类或方法返回的 MIME 类型 , 也就是资源方法产生并且返回给客户端的响应消息的类型。例如 application/xml. |
@Consumes |
该标签标注资源类或方法可以接受的请求消息的类型,也就是客户端发送的请求中可以包含的 Http Entity 的类型。 |
@PathParam,@QueryParam,@HeaderParam,@CookieParam |
该组分别标注方法的参数来自于 HTTP 请求的不同位置,例如,@PathParam 来自于 URL 的路径,@QueryParam 来自于 URL 的查询参数,@HeaderParam 来自于 HTTP 请求的头信息,@CookieParam 来自于 HTTP 请求的 Cookie。 |
@Context |
该标签标注资源请求或者响应的上下文实例,例如我们可以再资源方法参数中注入 HttpServletRequest (@Context HttpServletRequest httpRequest) 来获取 Http 请求的相关信息 |
Demo:
@GET @PATH(value=”/staff/query/{id}”) @Produces(“application/xml”) Staff getStaffById(@PathParam("id") int id)
C.restful webservice注解释义
@XmlAccessorType(XmlAccessType.
PROPERTY
) @XmlRootElement(name = "User") @XmlType(propOrder = { "userId", "nickname", "gender","registerDate"})
publicclassUser
implementsSerializable {
privatestaticfinallong
serialVersionUID= 1L;
privateString userId;
privateString nickname;
privateString gender;
privateDate registerDate;
publicUser(String userId, String nickname, String gender, Date registerDate)
{
this.userId = userId;
this.nickname = nickname;
this.gender = gender;
this.registerDate = registerDate; }
publicString getUserId()
{
returnuserId; }
publicvoidsetUserId(String userId)
{
this.userId = userId; }
publicString getNickname()
{
returnnickname; }
publicvoidsetNickname(String nickname)
{
this.nickname = nickname; }
publicString getGender()
{
returngender; }
publicvoidsetGender(String gender)
{
this.gender = gender; }
@XmlJavaTypeAdapter(DateConverter.
class
)
publicDate getRegisterDate() {
returnregisterDate; }
publicvoidsetRegisterDate(Date registerDate)
{
this.registerDate = registerDate; }
@Override
publicString toString()
{
return"User [userId=" + userId + ", nickname=" + nickname + ", gender=" + gender + ", registerDate =" + registerDate + "]"; } }
为了用 XML 映射多个用户,我们定义了 Users 类,Users 类只有一个成员变量,它是一个装有 User 对象的 java.util.List 列表。
@XmlRootElement(name = "Users"
)
publicclassUsers
{
@XmlElement(name = "User")
privateList users;
publicList getUsers()
{
returnusers; }
publicvoidsetUsers(List users)
{
this.users = users; } }
对于注册日期 registerDate 变量的映射,需要做一些必要的处理,我们希望日期被映射为 yyyy-mm-dd 的形 式,上述 User 类中, 我们用 @XmlJavaTypeAdapter 标签 注释 registerDate 的 getter 方法, 希望 告知 JAXB 遇到这个属性的时候, 用 DateConverter 类做处理
@Produces({ MediaType.
APPLICATION_XML
})
@Consumes({ MediaType.
APPLICATION_XML
})
publicinterface
UserService
{
@GET @Produces(MediaType.
APPLICATION_XML
) @Path("/searchuser/{userId}")
User searchUser(
@PathParam("userId")
String userId);
@POST @Consumes(MediaType.
APPLICATION_XML
) @Path("/adduser")
Response addUser(User user);
@DELETE @Consumes(MediaType.
APPLICATION_XML
) @Path("/deleteuser/{userId}")
Response deleteUser(
@PathParam("userId")
String userId);
@PUT @Path("/category") @Consumes(MediaType.
APPLICATION_XML
)
Response updateUser(User user);
@GET @Path("/getusers/startdate/{startDate}/enddate/{endDate}") @Produces({ MediaType.
APPLICATION_XML
})
Users getUsers(
@PathParam("startDate")
String startDate ,
@PathParam("endDate")
String endDate); }
publicclass
UserServiceImpl
implements
UserService
{
public
User searchUser(String userId)
{ User user = UserDAO.
searchUser
(userId);
if
(user ==
null
)
{
ResponseBuilder builder = Response.
status
(
Status.
NOT_FOUND
);
builder.type("application/xml"); builder.entity("User with id:" + userId + " can not be found!");
thrownew
WebApplicationException(builder.build()); }
else
{ System.
out
.println("User with id:" + userId + " is found");
return
user; } }
public
Response addUser(User user) { User userObj = (User) UserDAO.
searchUser
(user.getUserId());
if
(userObj !=
null
)
{
ResponseBuilder builder = Response.
status
(
Status.
FORBIDDEN
)
; builder.type("application/xml"); builder.entity("User with id:" + user.getUserId() + " already exists")
;
thrownew
WebApplicationException(builder.build()); }
else
{ UserDAO.
addUser
(user);
return
Response.
ok
(user).build();
} }
public
Response deleteUser(String userId) { User userObj = (User) UserDAO.
searchUser
(userId);
if
(userObj ==
null
)
{
ResponseBuilder builder = Response.
status
(
Status.
FORBIDDEN
); builder.type("application/xml"); builder.entity("User with id:" + userId + " is not existed, delettion request is rejected");
thrownew
WebApplicationException(builder.build()); }
else
{ UserDAO.
deleteUser
(userId);
return
Response.
ok
().build();
} }
public
Response updateUser(User user) { User userObj = (User) UserDAO.
searchUser
(user.getUserId());
if
(userObj ==
null
)
{
ResponseBuilder builder = Response.
status
(
Status.
FORBIDDEN
); builder.type("application/xml"); builder.entity("User with id:" + user.getUserId() + " is not existed, update request is rejected");
thrownew
WebApplicationException(builder.build()); }
else
{ UserDAO.
updateUser
(user);
return
Response.
ok
(user).build();
} }
public
Users getUsers(String startDate, String endDate) { List userList =
new
ArrayList();
ResponseBuilder builder = Response.
status
(Status.
OK
)
; builder.type("application/xml");
try
{ userList = UserDAO.
getUsersByRegDate
(startDate, endDate); }
catch
(Exception e) { e.printStackTrace();
builder = Response.
status
(S
tatus.
NOT_ACCEPTABLE
)
; builder.entity("" + "not accpertable date format for startDate or endDate");
thrownew
WebApplicationException(builder.build());
}
if
(userList.size() < 1) {
builder = Response.
status
(Status.
NOT_FOUND
)
; builder.entity("no user found registered between" +startDate+ " and " + endDate + "");
thrownew
WebApplicationException(builder.build()); }
else
{ Users users =
new
Users(); users.setUsers(userList);
return
users; } }
D.spring集成restful webService
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs=
"http://cxf.apache.org/jaxrs"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://camel.apache.org/schema/osgi http://camel.apache.org/schema/osgi/camel-osgi.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd"
>
"classpath:META-INF/cxf/cxf.xml"
/>
"userRestService"
address=
"/userservices/v1.0"
>
[
"userServiceImpl"
/>]
"userServiceImpl"
class=
"com.netprovider.user.service.baseinfo.impl.UserServiceImpl"
>
E. 客户端调用
1.
添加:post请求
private static void testAddUser()
{
WebClient client = WebClient.create(SERVICE_URL);
client.path("/adduser").accept(MIME_TYPE).type(MIME_TYPE);
User user = new User(); user.setUserId("00" + new Random().nextInt(10000)); user.setGender("Male"); user.setNickname("Who am I"); user.setRegisterDate(new Date());
User addedUser = client.post(user, User.class);
System.out.println("added user id:" + addedUser.getUserId()); assertResult(user.getUserId(),addedUser.getUserId()); //set TEST_USER_ID to the one added, so that the user //which is added for testing can be deleted later TEST_USER_ID = addedUser.getUserId(); System.out.println("Test addUser method successfully");}
2.查询:get请求
private static void testGetUsers() {
WebClient client = WebClient.create(SERVICE_URL);
//according to the tested data we insert,during this time period //only one person registered
Users users = client.path("/getusers/startdate/2012-10-17/ enddate/2012-10-19") .accept(MIME_TYPE).type(MIME_TYPE).get(Users.class);
assertResult("1", String.valueOf(users.getUsers().size())); System.out.println("Test getUsers method successfully");}
3.修改:put请求
private static void testUpdateUser() {
WebClient client = WebClient.create(SERVICE_URL);
User user = new User(); user.setUserId(TEST_USER_ID); user.setGender("Female"); user.setNickname("Test Nickname"); user.setRegisterDate(new Date());
Response response = client.path("/updateuser") .accept(MIME_TYPE).type(MIME_TYPE).put(user);
assertResult("200", String.valueOf(response.getStatus())); System.out.println("Test updateUser method successfully"); }
4.删除:delete请求
private static void testDeleteUser() {
WebClient client = WebClient.create(SERVICE_URL);
Response response = client.path("/deleteuser/" + TEST_USER_ID) .accept(MIME_TYPE).type(MIME_TYPE).delete();
assertResult("200", String.valueOf(response.getStatus())); System.out.println("Test getUsers method successfully");} private static void assertResult(String expectedResult , String realResult) { if (expectedResult != realResult && !expectedResult.equals(realResult)) { throw new RuntimeException("Test failed,excepted result:" + expectedResult + " real returned result:" + realResult); } }}
F.客户端配置和spring集成
-
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:oscache="http://www.springmodules.org/schema/oscache"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
G. 注释(Annotation):在 javax.ws.rs.* 中定义,是 JAX-RS (JSR 311) 规范的一部分。
@Path:定义资源基 URI。由上下文根和主机名组成,资源标识符类似于 http://localhost:8080/RESTful/rest/hello。
@GET:这意味着以下方法可以响应 HTTP GET 方法。
@Produces:以纯文本方式定义响应内容 MIME 类型。
@Context: 使用该注释注入上下文对象,比如 Request、Response、UriInfo、ServletContext 等。
@Path("{contact}"):这是 @Path 注释,与根路径 “/contacts” 结合形成子资源的 URI。
@PathParam("contact"):该注释将参数注入方法参数的路径,在本例中就是联系人 id。其他可用的注释有 @FormParam、@QueryParam 等。
@Produces:响应支持多个 MIME 类型。在本例和上一个示例中,APPLICATION/XML 将是默认的 MIME 类型。
@Consumes:声明该方法使用 HTML FORM。
@FormParam:注入该方法的 HTML 属性确定的表单输入。
@Response.created(uri).build(): 构建新的 URI 用于新创建的联系人(/contacts/{id})并设置响应代码(201/created)。