【webservice】spring整合webservice RS风格
【webservice】spring整合webservice RS风格
UserService
package cn.zxl.cxf.service;import java.util.List;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 cn.zxl.cxf.domain.User;@Path("/userService")
@Produces("*/*")
public interface IUserService {@POST@Path("/user")@Consumes({ "application/xml", "application/json" })public void saveUser(User user);@PUT@Path("/user")@Consumes({ "application/xml", "application/json" })public void updateUser(User user);@GET@Path("/user")@Produces({ "application/xml", "application/json" })public List findAllUsers();@GET@Path("/user/{id}")@Consumes("application/xml")@Produces({ "application/xml", "application/json" })public User finUserById(@PathParam("id") Integer id);@DELETE@Path("/user/{id}")@Consumes("application/xml")public void deleteUser(@PathParam("id") Integer id);
}
UserServiceImpl
package cn.zxl.cxf.service;
import java.util.ArrayList;
import java.util.List;
import cn.zxl.cxf.domain.Car;
import cn.zxl.cxf.domain.User;public class UserServiceImpl implements IUserService {public void saveUser(User user) {System.out.println("save user:" + user);}public void updateUser(User user) {System.out.println("update user:" + user);}public List findAllUsers() {List users = new ArrayList();User user1 = new User();user1.setId(1);user1.setUsername("小明");user1.setCity("北京");List carList1 = new ArrayList();Car car1 = new Car();car1.setId(101);car1.setCarName("保时捷");car1.setPrice(1000000d);carList1.add(car1);Car car2 = new Car();car2.setId(102);car2.setCarName("宝马");car2.setPrice(400000d);carList1.add(car2);user1.setCars(carList1);users.add(user1);User user2 = new User();user2.setId(2);user2.setUsername("小丽");user2.setCity("上海");users.add(user2);return users;}public User finUserById(Integer id) {if (id == 1) {User user1 = new User();user1.setId(1);user1.setUsername("小明");user1.setCity("北京");return user1;}return null;}public void deleteUser(Integer id) {System.out.println("delete user id :" + id);}}
applicationContext.xml
web.xml
contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener CXFService org.apache.cxf.transport.servlet.CXFServlet 1 CXFService /services/* index.html index.htm index.jsp default.html default.htm default.jsp
pom.xml
4.0.0 cn.itcast.maven cxf_rs_spring 0.0.1-SNAPSHOT war cxf_rs_spring CXF的rs服务发布与spring整合 org.apache.cxf cxf-rt-frontend-jaxrs 3.0.1 org.slf4j slf4j-log4j12 1.7.12 org.apache.cxf cxf-rt-rs-client 3.0.1 org.apache.cxf cxf-rt-rs-extension-providers 3.0.1 org.codehaus.jettison jettison 1.3.7 org.springframework spring-context 4.1.7.RELEASE org.springframework spring-web 4.1.7.RELEASE org.springframework spring-test 4.1.7.RELEASE junit junit 4.12 org.codehaus.mojo tomcat-maven-plugin 1.1 9996
domain:
car
package cn.zxl.cxf.domain;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "Car")
public class Car {private Integer id;private String carName;private Double price;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCarName() {return carName;}public void setCarName(String carName) {this.carName = carName;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic String toString() {return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";}}
domain:
user
package cn.zxl.cxf.domain;import java.util.ArrayList;
import java.util.List;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "User")
public class User {private Integer id;private String username;private String city;private List cars = new ArrayList();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public List getCars() {return cars;}public void setCars(List cars) {this.cars = cars;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", city=" + city + ", cars=" + cars + "]";}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
