WebService-CXF
CXF:也是用来开发WebService的。不依赖于任何的服务端端的接口和类
收到的是xml文件,但是需要自己去解析xmlw文件。
1、CXF也能整合到SpringBoot中去的。
1.1、搭建Spring CXF+RESTFUL风格的WebService
1.2、引入相关的依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--CXF相关包-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!-- web service -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<spring.version>4.3.18.RELEASE</spring.version>
<jackson.version>2.9.6</jackson.version>
<cxf.version>3.2.7</cxf.version>
1.3、编写WebService终端接口的API和实体类的代码如下:
package com.xf.demo.service;
import com.xf.demo.domain.User;
import javax.jws.WebService;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
@WebService
@Path(value="/users/")
public interface UserService {
@GET
@Path("/") //http://ip:port/users
@Produces({MediaType.APPLICATION_JSON})
List<User> getUsers();
@DELETE
@Path("{id}") //http://ip:port/users/1
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}) //请求accept
Response delete(@PathParam("id") int id);
@GET
@Path("{id}") //http://ip:port/users/1
@Produces(MediaType.APPLICATION_JSON)
User getUser(@PathParam("id") int id);
@POST
@Path("/add")
@Produces({MediaType.APPLICATION_JSON})
Response add(User user);
@PUT
@Path("/update")
Response update(User user);
}
package com.xf.demo.service;
import com.xf.demo.domain.User;
import com.xf.demo.result.Response;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@Service
public class UserServiceImpl implements UserService {
public static List<User> users=null;
static{
users=new CopyOnWriteArrayList<>();
users.add(new User(1001,"张三",20,new Date()));
users.add(new User(1002,"王五",21,new Date()));
users.add(new User(1003,"赵六",22,new Date()));
users.add(new User(1004,"田七",23,new Date()));
}
@Override
public List<User> getUsers() {
return users;
}
@Override
public Response delete(int id) {
int size=users.size();
for(User user:users){
if(user.getId().intValue()==id){
users.remove(user);
}
}
if(users.size()<size){
return new Response(0,"success",null);
}else{
return new Response(1,"fail",null);
}
}
@Override
public User getUser(int id) {
return users.get(id-1);
}
@Override
public Response add(User user) {
users.add(user);
return new Response(0,"success",null);
}
@Override
public Response update(User user) {
return null;
}
}
cxf格式的webService不是把接口发不出去,而是像RestFul形式的去访问。
cxf返回的是即是json也可以是xml文件,因为在cxf中提供了转换器可以把xml文件转换成json的格式
1.4、发布下:这就是restful的json
1.5、cxf这种方式可以设置不同的请求头,来控制 返回的是xml还是json,比前几种更方便。
1.6、如果想要获取的是xml的话:需要在在实体类上加上注解:
1.7、在次访问下delete的方法,请求的结果如下:
革命尚未成功,继续努力!!!!