博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Apache CXF搭建REST风格的Web Service
阅读量:5121 次
发布时间:2019-06-13

本文共 4140 字,大约阅读时间需要 13 分钟。

1)编写domain实体类

package com.domain;import java.io.Serializable;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name="UserInfo")public class User implements Serializable,Comparable
{ /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private String email; private String address; public User(){} @XmlElement public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @XmlElement public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return this.name+","+this.email+","+this.address; } @Override public int compareTo(User other) { return this.id - other.id; }}

2)业务处理接口

package com.service;import com.domain.User;public interface IUserService {
public User getUserById(int id);}

3)业务处理实现类

package com.service;import com.domain.User;public class UserService implements IUserService{
public User getUserById(int id){ User user = new User(); user.setId(1); user.setName("Rickesy"); user.setAddress("china"); user.setEmail("Rickesy@163.com"); return user; };}

3)Web Service接口

package com.rest;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import com.domain.User;@Path(value="/")public interface IRESTService {    @GET    @Path("user/{id}")    @Produces({MediaType.APPLICATION_XML,        MediaType.APPLICATION_JSON})    public User getUserById(@PathParam("id") int id);}

4)Web Service实现类

package com.rest;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import com.domain.User;import com.service.UserService;@Path(value="/")public class RESTService implements IRESTService{
private User user; private IUserService userService; @GET @Path("user/{id}") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public User getUserById(@PathParam("id") int id){ return userService.getUserById(id); } public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }

5)配置applicationContext.xml文件

6)配置web.xml

org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
CXFServlet
/*

7)客户端测试

package com.client;import javax.ws.rs.core.MediaType;import org.apache.cxf.jaxrs.client.WebClient;public class Client {    static WebClient client = WebClient.create(        "http://localhost:8080/RESTWebService/rest");    public static void main(String[] args) {        User user = client.path("/user/{id}",             1).accept(MediaType.APPLICATION_XML).get(User.class);        System.out.println(user);    }}

转载于:https://www.cnblogs.com/clongxiang/p/4321543.html

你可能感兴趣的文章
django 单独测试模块
查看>>
mac OS环境下的PHP环境配置
查看>>
HIVE数据操作
查看>>
Saving Activity state in Android
查看>>
SquirrelMQ消息队列
查看>>
剑指Offer_编程题_4
查看>>
有多少人忽视了这简单的道理,又有多少人觉得理所當然。。。。
查看>>
练习笔记:net,JqueryUI实现自动补全功能
查看>>
[转帖]SQL中partition关键字的使用
查看>>
小程序实战小汇总
查看>>
inner join 与 left join 之间的区别
查看>>
系统对接API调用
查看>>
POJ 3398 Perfect Service(树型动态规划,最小支配集)
查看>>
Servlet的生命周期和工作原理
查看>>
【树链剖分模板】bzoj1036 树的统计
查看>>
一些作业
查看>>
ajax使用异步问题
查看>>
唯有志存高远,方能风行天下
查看>>
Linux产生序列数字
查看>>
【循序渐进学Python】9.异常处理
查看>>