1. 定义 JavaBean。注意 @XmlRootElement 注解,作用是将 JavaBean 映射成 XML 元素。
package com.huey.demo.bean; import javax.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
public class Book { private String title;
private String author;
private String publisher;
private String isbn; }
package com.huey.demo.bean; import javax.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
public class ResultMsg { private String resultCode;
private String message; }
2. 定义服务接口。注意各个注解的作用。
package com.huey.demo.ws; import java.util.List; import javax.jws.WebService;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import com.huey.demo.bean.Book;
import com.huey.demo.bean.ResultMsg; @WebService
public interface BookService { @GET // 指定请求方式
@Path("/book/{isbn}") // 指定资源的 URI
@Produces( { MediaType.APPLICATION_XML } ) // 指定请求/响应的媒体类型
public Book getBook(@PathParam("isbn") String isbn); @GET
@Path("/books")
@Produces( { MediaType.APPLICATION_XML } )
public List<Book> getBooks(); @POST
@Path("/book")
@Produces( { MediaType.APPLICATION_XML } )
public ResultMsg addBook(Book book); @PUT
@Path("/book/{isbn}")
@Produces( { MediaType.APPLICATION_XML } )
public ResultMsg updateBook(@PathParam("isbn") String isbn, Book book); @DELETE
@Path("/book/{isbn}")
@Produces( { MediaType.APPLICATION_XML } )
public ResultMsg deleteBook(@PathParam("isbn") String isbn);
}
3. 实现服务接口。
package com.huey.demo.ws.impl; import java.util.ArrayList;
import java.util.List; import javax.jws.WebService; import org.apache.commons.lang.StringUtils; import com.huey.demo.bean.Book;
import com.huey.demo.bean.ResultMsg;
import com.huey.demo.ws.BookService;
import com.sun.org.apache.commons.beanutils.BeanUtils; @WebService
public class BookServiceImpl implements BookService { List<Book> books = new ArrayList<Book>(); public BookServiceImpl() {
books.add(new Book("嫌疑人X的献身", "东野圭吾", "南海出版公司", "9787544245555"));
books.add(new Book("追风筝的人", "卡勒德·胡赛尼 ", "上海人民出版社", "9787208061644"));
books.add(new Book("看见", "柴静 ", "广西师范大学出版社", "9787549529322"));
books.add(new Book("白夜行", "东野圭吾", "南海出版公司", "9787544242516"));
} public Book getBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
return book;
}
}
return null;
} public List<Book> getBooks() {
return books;
} public ResultMsg addBook(Book book) {
if (book == null || StringUtils.isEmpty(book.getIsbn())) {
return new ResultMsg("FAIL", "参数不正确!");
}
if (getBook(book.getIsbn()) != null) {
return new ResultMsg("FAIL", "该书籍已存在!");
}
books.add(book);
return new ResultMsg("SUCCESS", "添加成功!");
} public ResultMsg updateBook(String isbn, Book book) {
Book target = getBook(isbn);
if (target != null) {
ResultMsg resultMsg = new ResultMsg("SUCCESS", "更新成功!");
try {
BeanUtils.copyProperties(target, book);
} catch (Exception e) {
resultMsg = new ResultMsg("FAIL", "未知错误!");
}
return resultMsg;
}
return new ResultMsg("FAIL", "该书籍不存在!");
} public ResultMsg deleteBook(String isbn) {
Book book = getBook(isbn);
if (book != null) {
books.remove(book);
return new ResultMsg("SUCCESS", "删除成功!");
}
return new ResultMsg("FAIL", "该书籍不存在!");
}
}
4. Spring 配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <bean id="bookRestService" class="com.huey.demo.ws.impl.BookServiceImpl" /> <jaxrs:server id="bookService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="bookRestService" />
</jaxrs:serviceBeans>
</jaxrs:server> </beans>
5. web.xml 配置。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.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>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
6. 启动 Tomcat 运行 web 工程。
7. 测试。
a) getBooks
b) getBook
c) addBook
d) updateBook
e) deleteBook