webService之helloword(java)rs
webservice之rs(helloworld)
1.pom.xml文件
<dependencies>
<!-- 使用CXF RS开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency> <!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency> <!-- 使用log4j日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency> <!-- 在CXF扩展提供者,提供转换json接口 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.1</version>
</dependency> <!-- cxf 扩展提供者 转换json 默认需求一个工具包 -->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
2.创建服务接口
package com.baidu.service; import javax.ws.rs.Consumes;
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.QueryParam; import com.baidu.domain.User; @Path("/userService")//服务器访问资源路径
//@Produces("*/*")// 生成 方法的返回值 //指定能否生成哪种格式数据返回给客户端
public interface UserService {
//@GET 查询 @PUT 修改 @POST 增加 @DELETE 删除 @POST
@Path("/saveUser")
@Consumes({"application/xml","application/json"}) //消费 方法的参数
public void saveUser(User user);
@DELETE
@Path("/deleteUser")
@Consumes({"application/xml","application/json"})
public void deleteUser(@QueryParam(value="id")Integer id);
@PUT
@Path("/updateUser")
@Consumes({"application/xml","application/json"})
public void updateUser(User user);
@GET
@Path("/queryUser/{id}")
@Consumes({"application/xml","application/json"})
@Produces({"application/xml","application/json"})
public User queryUser(@PathParam(value="id")Integer id);
}
| @Path | 服务器访问资源路径 |
| @Produces | 指定生成那种格式的额数据格式返回给客户端 |
| @Consumes | 指定接收的数据格式 |
| @POST | 指定该方法为保存(保存) |
| @DELETE | 指定该方法为删除 |
| @PUT | 指定该方法为修改 |
| @GET | 指定该方法为查询 |
3.创建服务实现类
package com.baidu.service.imp; import com.baidu.domain.User;
import com.baidu.service.UserService; public class UserServiceImp implements UserService {
public void saveUser(User user) {
System.out.println("saveUser"+user);
}
public void deleteUser(Integer id) {
System.out.println("deleteUser id:"+id);
}
public void updateUser(User user) {
System.out.println("updateUser user:"+user);
}
public User queryUser(Integer id) {
System.out.println("id");
if(id==1){
System.out.println(id);
User u=new User();
u.setId(id);
u.setName("zhangsan");
return u;
}
return null;
}
}
4.发布服务
package com.baidu.publish; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import com.baidu.domain.User;
import com.baidu.service.UserService;
import com.baidu.service.imp.UserServiceImp; public class PublishService {
public static void main(String[] args) {
UserService us=new UserServiceImp();
JAXRSServerFactoryBean serverFactoryBean=new JAXRSServerFactoryBean();
serverFactoryBean.setResourceClasses(User.class);
serverFactoryBean.setServiceBean(us);
serverFactoryBean.setAddress("http://localhost:8099");
serverFactoryBean.create();
}
}
测试项目的pom.xml文件
<dependencies>
<!-- 使用CXF RS开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency> <!-- 使用log4j日志实现 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency> <!-- 使用rs客户端 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.1</version>
</dependency> <!-- 在CXF扩展提供者,提供转换json接口 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.1</version>
</dependency> <!-- cxf 扩展提供者 转换json 默认需求一个工具包 -->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
测试发布的服务
public static void main(String[] args) {
// 保存
// User user = new User();
// user.setId(1);
// user.setName("zhangsan");
// WebClient.create("http://localhost:8099/userService/saveUser")
// .type(MediaType.APPLICATION_XML).post(user);
// 删除
// WebClient.create("http://localhost:8099/userService/deleteUser?id=1").type(MediaType.APPLICATION_XML).delete();
// 修改
// User user = new User();
// user.setId(1);
// user.setName("zhangsan");
// WebClient.create("http://localhost:8099/userService/deleteUser").type(MediaType.APPLICATION_XML).put(user);
// 查询
// User user =WebClient.create("http://localhost:8099/userService/queryUser/1")
// .accept(MediaType.APPLICATION_XML).get(User.class);
// System.out.println(user);
}
User类
package com.baidu.domain; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="User")
public class User { private Integer Id;
private String name;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [Id=" + Id + ", name=" + name + "]";
} }
webService之helloword(java)rs的更多相关文章
- webservice之helloword(web)rs
spring整合webservice 1.pom.xml文件 <dependencies> <!-- cxf 进行rs开发 必须导入 --> <dependency> ...
- webService之helloword(java)
webservice 远程数据交互技术 1.导入jar包(如果是 maven项目导入项目坐标) 2.创建服务 3.测试服务 我们使用maven来做测试服务 pom.xml文件 <project ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- C#动态webservice调用接口 (JAVA,C#)
C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using ...
- Axis2 webservice 之使用java调用webservice
在上一篇中写了一个简单了webservice,实现了一个sayHello功能.那么webservice写好之后我们如何使用Java程序来调用webservice呢? 一.java调用的webservi ...
- php调用webservice接口,java代码接收不到参数
前段时间做了一个项目的二次开发,有个功能是需要php调用java实现的webservice接口,并传递一些参数给接口,然后按照对方提供的接口说明文档进行传参调用,java那边有接收到请求,但是参数总是 ...
- WebService应用--使用java开发WebService程序
使用Eclipse开发第一个WebService程序,本示例采用的工具为Spring-Tool-Suite,和Eclipse没有本质的区别,开发环境jdk1.7 一.开发步骤: 1.新建名为WebSe ...
- webService之helloword(web)
spring 整合webservice pom.xml文件 <dependencies> <!-- CXF WS开发 --> <dependency> <gr ...
- 做Webservice时报错java.util.List是接口, 而 JAXB 无法处理接口。
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExc ...
随机推荐
- ajax添加header信息
$.ajax({url:"xxx",async:true,dataType:"json",contentType:"application/json& ...
- 左侧菜单栏,有对个li对应一个content
html部分截图 不多说直接上js /*左侧导航栏*/var sect=$(".sect"); $(".nav-list .nav-a").each(funct ...
- 为Linux虚拟机设置网络
安装虚拟机的时候为了使用方便我们除了需要设置静态ip为了能够让虚拟机也能够上网我们需要设置虚拟机网络 当然也可以使用虚拟机和主机共享上网,这个比较简单,这里就不说了,现在我们来通过桥接的方式为虚拟机设 ...
- Oracle_高级功能(1) 数据库设计
1.三范式规范化关系模式称为范式.第一范式:在一个关系模型R中,如果R的每一个属性的值域中的值都是不可再分的最小数据单位, 则称R为第一范式(1NF).第二范式:如果一个关系模型R属于1NF,并且R的 ...
- js iterable类型
遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...
- 4C - 七夕节
七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" 人们纷纷来到告示前,都想知道谁才是自己 ...
- oracle 直接复制表内容到新表
不知道为什么,刚建的oracle数据库删除数据很慢,表里面有120多万数据,非常地慢 于是采用的复制的方法,命令如下: create table students_backup as select * ...
- andorid 练习微信登陆
AndroidManifest.xml layout1.xml <?xml version="1.0" encoding="utf-8"?> < ...
- Kali Linux 网络扫描秘籍
第三章 端口扫描(二) 作者:Justin Hutchens 译者:飞龙 协议:CC BY-NC-SA 4.0 3.6 Scapy 隐秘扫描 执行 TCP 端口扫描的一种方式就是执行一部分.目标端口上 ...
- Vsphere初试——使用Vsphere client
好不容易安装好ESXi之后,就要安装一个Vsphere Client,为什么要安装这个东东.使用过vmware workstation的人都知道,安装完就可以添加虚拟机,但是ESXi要通过Vspher ...