上篇博客简单介绍了Restful Service的服务端编程,本篇博客详细的介绍了通过Spring集成发布服务的过程。
一、实现工具: Eclipse
二、实现过程及代码:
1、使用Maven创建web项目cxf-rs-spring
File -> New -> Others -> Maven -> Maven Project -> use default ... -> maven archetype webapp
2、配置pom.xml文件 添加依赖
4.0.0 me.cxf-sample cxf-rs-spring war 0.0.1-SNAPSHOT cxf-rs-spring Maven Webapp http://maven.apache.org 9.3.7.v20160115 3.1.6 junit junit 3.8.1 test javax.ws.rs javax.ws.rs-api 2.0.1 org.apache.cxf cxf-rt-transports-http ${cxf.version} org.apache.cxf cxf-rt-frontend-jaxrs ${cxf.version} org.apache.cxf cxf-rt-rs-service-description ${cxf.version} org.springframework spring-web 3.2.8.RELEASE org.codehaus.jackson jackson-jaxrs 1.9.13 org.codehaus.jackson jackson-xc 1.9.13org.slf4jslf4j-jdk141.7.19org.slf4jjcl-over-slf4j1.7.19 cxf-rs-spring org.eclipse.jetty jetty-maven-plugin ${jettyVersion} 3、找到文件/src/main/webapp/WEB-INF/web.xml 修改为 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > cxf-rs-spring Maven Webapp contextConfigLocation WEB-INF/beans.xml org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener CXFServlet org.apache.cxf.transport.servlet.CXFServlet CXFServlet /*
4、在相同目录下(/src/main/webapp/WEB-INF/),新建beans.xml文件
创建项目,这时发现没有 src/main/java 源代码目录。 按以下步骤操作:项目名 -> Context Menu(鼠标右键) -> properties -> Java Build Path -> JRE System Lib -> Edit按钮 -> Workspace default JRE
根资源类(CustomerService):
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package demo.jaxrs.server;import java.util.HashMap;import java.util.Map;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 javax.ws.rs.core.Response;@Path("/customerservice/")//@Produces("text/xml")//@Produces("application/json")@Produces({ "text/plain","text/xml", "application/json", "application/xml", "application/x-www-form-urlencoded"})@Consumes({ "application/x-www-form-urlencoded", "text/plain","text/xml", "application/json" })public class CustomerService { long currentId = 123; Map customers = new HashMap(); Map orders = new HashMap(); public CustomerService() { init(); } @GET @Path("/customers/{id}/") public Customer getCustomer(@PathParam("id") String id) { System.out.println("----invoking getCustomer, Customer id is: " + id); long idNumber = Long.parseLong(id); Customer c = customers.get(idNumber); return c; } @PUT @Path("/customers/") public Response updateCustomer(Customer customer) { System.out.println("----invoking updateCustomer, Customer name is: " + customer.getName()); Customer c = customers.get(customer.getId()); Response r; if (c != null) { customers.put(customer.getId(), customer); r = Response.ok().build(); } else { r = Response.notModified().build(); } return r; } @POST @Path("/customers/") public Response addCustomer(Customer customer) { System.out.println("----invoking addCustomer, Customer name is: " + customer.getName()); customer.setId(++currentId); customers.put(customer.getId(), customer); return Response.ok(customer).build(); } @DELETE @Path("/customers/{id}/") public Response deleteCustomer(@PathParam("id") String id) { System.out.println("----invoking deleteCustomer, Customer id is: " + id); long idNumber = Long.parseLong(id); Customer c = customers.get(idNumber); Response r; if (c != null) { r = Response.ok().build(); customers.remove(idNumber); } else { r = Response.notModified().build(); } return r; } @Path("/orders/{orderId}/") public Order getOrder(@PathParam("orderId") String orderId) { System.out.println("----invoking getOrder, Order id is: " + orderId); long idNumber = Long.parseLong(orderId); Order c = orders.get(idNumber); return c; } final void init() { Customer c = new Customer(); c.setName("John"); c.setId(123); customers.put(c.getId(), c); Order o = new Order(); o.setDescription("order 223"); o.setId(223); orders.put(o.getId(), o); }}子资源类:
Order类:
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package demo.jaxrs.server;import java.util.HashMap;import java.util.Map;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "Order")public class Order { private long id; private String description; private Map products = new HashMap(); public Order() { init(); } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String d) { this.description = d; } @GET @Path("products/{productId}/") public Product getProduct(@PathParam("productId")int productId) { System.out.println("----invoking getProduct with id: " + productId); Product p = products.get(new Long(productId)); return p; } final void init() { Product p = new Product(); p.setId(323); p.setDescription("product 323"); products.put(p.getId(), p); }}Produce类:/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package demo.jaxrs.server;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "Product")public class Product { private long id; private String description; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String d) { this.description = d; }}最后文件目录结构为6、运行项目
Maven运行参数为 package jetty:run
7、 运行结果:
GET 请求
POST请求
8、 如果要输出JSON格式的文本,需要在pom.xml 和beans.xml中添加依赖, 上述代码中已添加,可自行查看
运行结果如下: