服务提供方RS xml或者json
2.5.1 编写服务端
第一步:创建maven的web工程,导入一下依赖
<dependencies>
<!-- 导入webservice依赖 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.colobu</groupId>
<artifactId>fastjson-jaxrs-json-provider</artifactId>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>1234</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
第二步:在web.xml中配置spring容器在应用加载时创建
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
第三步:在web.xml中配置CXF的核心控制器
<!-- 配置CXF的核心控制器,就是一个Servlet:CXFServlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<!-- 此处不要配置/*,因为CXFServlet只能处理webservice请求 /ws/*表明请求URL中有/ws的请求 -->
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
第四步:业务代码
实体类代码
package cn.itcast.pojo;
/*<user>
<id>11</id>
<name>zhangsan</name>
</user>*/
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlRootElement(name="user") //规定了xml中root节点的名称,此注解不能缺少
public class User implements Serializable {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接口代码
package cn.itcast.service;
import cn.itcast.pojo.User;
import javax.ws.rs.*;
@Path("/user") //@Path 相当于@RequestMapping
public interface UserService {
@POST
@Path("/")
@Consumes() //消费者请求时的参数数据格式
public void save(User user);
@PUT
@Path("/")
@Consumes(value ={"application/json","application/xml"})
public void update(User user);
@GET
@Path("/{id}")
@Consumes(value ={"application/json","application/xml"})
@Produces(value ={"application/xml","application/json"}) //提供者返回时的内容数据格式
public User findById(@PathParam("id") Integer id); //@PathParam从路径中获取参数。此注解不能少
@DELETE
@Path("/{id}")
@Consumes(value ={"application/json","application/xml"})
public void deleteById(@PathParam("id")Integer id);
}
实现类代码
package cn.itcast.service.impl;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void save(User user) {
System.out.println("进入了save方法"+user.getName());
}
@Override
public void update(User user) {
System.out.println("进入了update方法"+user.getName());
}
@Override
public User findById(Integer id) {
User user = new User();
user.setId(id);
user.setName("zhangsan");
return user;
}
@Override
public void deleteById(Integer id) {
System.out.println("进入了deleteById方法:id是"+id);
}
}
第四步:启动项目