First, create a server
1. Add dependence
<dependencies> <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-transports-http-jetty</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</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>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
2. Create entity classes
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Car") public class Car { private Integer id; private String carName; private Double price; }
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "User") public class User { private Integer id; private String username; private String city; }
3. Create a service interface
Import javax.ws.rs *. ; Import java.util.List; / ** * address method to access the current @Path designated service interface or interfaces * @Produces designated support response data content formats * @POST modification method , the current request shows another embodiment similar method supported .PUT / GET, etc. * @Consumes support request format * / @Path ( "/ that userService" ) @Produces ( "* / *") // support response for all data format public interface UserService { @POST @Path ( "/ User" ) @Consumes ({ "file application / XML", "file application / JSON" }) // representation support data xml / json request void Save (the User User ); @PUT @Path ("/user") @Consumes({"application/xml", "application/json"}) void update(User user); @GET @Path("/user") @Produces({"application/xml", "application/json"}) List<User> findAll(); @GET @Path("/user/{id}") @Consumes("application/xml") @Produces({"application/xml", "application/json"}) User findById(@PathParam("id") Integer id); @DELETE @Path("/user/{id}") @Consumes({"application/xml", "application/json"}) void delete(@PathParam("id") Integer id); }
4. Create a service interface
import java.util.ArrayList; import java.util.List; public class UserServiceImpl implements UserService { public void save(User user) { System.out.println("save user:" + user); } public void update(User user) { System.out.println("update user:" + user); } public List<User> findAll() { List<User> users = new ArrayList<>(); User user1 = new User(); user1.setId(1); user1.setUsername("小明"); user1.setCity("北京"); List<Car> carList = new ArrayList<>(); Car car1 = new Car(); car1.setId(101); car1.setCarName("保时捷"); car1.setPrice(1000000d); carList.add(car1); Car car2 = new Car(); car2.setId(102); car2.setCarName("宝马"); car2.setPrice(400000d); carList.add(car2); user1.setCars(carList); users.add(user1); User user2 = new User(); user2.setId(2); user2.setUsername("小丽"); user2.setCity("上海"); users.add(user2); return users; } public User findById(Integer id) { if (id == 1) { User user = new User(); user.setId(1); user.setUsername("小明"); user.setCity("北京"); return user; } return null; } public void delete(Integer id) { System.out.println("delete user id :" + id); } }
5. Publishing Service
Import cn.itcast.service.UserServiceImpl; Import org.apache.cxf.interceptor.LoggingInInterceptor; Import org.apache.cxf.interceptor.LoggingOutInterceptor; Import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; public class WebServiceServer { / ** * webservice server publishing service * / public static void main (String [] args) { // based server factory object JAX-RS protocol JAXRSServerFactoryBean FactoryBean = new new JAXRSServerFactoryBean (); // set service address factoryBean.setAddress ( "http: localhost //: 12345 / " ); // set the service class factoryBean.setServiceBean ( new new UserServiceImpl ()); // Set the log input, the output of the interceptor, can observe the log information factoryBean.getInInterceptors () the Add (. new new LoggingInInterceptor ()); factoryBean.getOutInterceptors () the Add (. new new LoggingOutInterceptor () ); // create a service factoryBean.create (); } }
Second, create a client
1. Call Service
Import org.apache.cxf.jaxrs.client.WebClient; Import org.junit.Test; Import javax.ws.rs.core.MediaType; Import java.util.Collection; public class WebServiceClient { / ** * client calls WebService service * / // request the default xml format @Test public void Save () { the User User = new new the User (); user.setId ( 10 ); user.setUsername ( "Bei" ); // the WebClient client call object is webservice WebClient.create ( "http: // localhost: 12345 / userService / user") .post(user); } // 指定请求数据为json格式 @Test public void update() { User user = new User(); user.setUsername("关羽"); user.setId(12); user.setCity("荆州"); WebClient.create("http://localhost:12345/userService/user") .type(MediaType.APPLICATION_JSON) .put(user); } @Test public void findById() { User user = WebClient.create("http://localhost:12345/userService/user/1") .type(MediaType.APPLICATION_JSON) //指定请求数据格式 .accept(MediaType.APPLICATION_JSON)//响应数据格式 .get(User.class); System.out.println(user); } @Test public void findAll() { Collection<? extends User> users = WebClient.create("http://localhost:12345/userService/user") .getCollection(User.class); System.out.println(users); } @Test public void delete() { WebClient.create("http://localhost:12345/userService/user/1") .delete(); } }