【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


contextConfigLocationclasspath:applicationContext.xmlorg.springframework.web.context.ContextLoaderListenerCXFServiceorg.apache.cxf.transport.servlet.CXFServlet1CXFService/services/*index.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jsp

pom.xml

4.0.0cn.itcast.mavencxf_rs_spring0.0.1-SNAPSHOTwarcxf_rs_springCXF的rs服务发布与spring整合org.apache.cxfcxf-rt-frontend-jaxrs3.0.1org.slf4jslf4j-log4j121.7.12org.apache.cxfcxf-rt-rs-client3.0.1org.apache.cxfcxf-rt-rs-extension-providers3.0.1org.codehaus.jettisonjettison1.3.7org.springframeworkspring-context4.1.7.RELEASEorg.springframeworkspring-web4.1.7.RELEASEorg.springframeworkspring-test4.1.7.RELEASEjunitjunit4.12org.codehaus.mojotomcat-maven-plugin1.19996

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 + "]";}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部