cxf soap rest webservice spring
1. 导入 jar 包
2. 编写接口
3. 编写实现
4. 配置spring 配置文件
5. 配置web.xml servlet
6. 访问
package com.diancai.test; import javax.jws.WebParam; import javax.jws.WebService; import com.diancai.service.dcrestaurant.WsConstants; @WebService(name="TestHelloword", targetNamespace=WsConstants.NS) public interface TestHelloword { String sayHi(@WebParam(name="name")String name); }
package com.diancai.test; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/testHellowordRest") public interface TestHellowordRest { @GET @Path("/say/{name}") @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }) // 这里面的值是返回值能接受的类型 public String sayHello(@PathParam("name")String name); @GET @Path("/sayHi/{name}") @Produces(MediaType.APPLICATION_XML) public HelloName sayHi(@PathParam("name")String name); }
package com.diancai.test.impl; import javax.jws.WebService; import com.diancai.test.TestHelloword; import com.diancai.service.dcrestaurant.WsConstants; @WebService(serviceName="TestHelloword",endpointInterface="com.diancai.test.TestHelloword",targetNamespace=WsConstants.NS) public class TestHellowordImpl implements TestHelloword{ /** * 注入service */ @Override public String sayHi(String name) { return "hello," +name; } }
package com.diancai.test.impl; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.diancai.test.HelloName; import com.diancai.test.TestHellowordRest; @Path("/testHellowordRest") public class TestHellowordRestImpl implements TestHellowordRest{ @GET @Path("/say/{name}") @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }) public String sayHello(@PathParam("name")String name) { return "hello,"+name; } @GET @Path("/sayHi/{name}") @Produces(MediaType.APPLICATION_XML) public HelloName sayHi(@PathParam("name")String name){ HelloName a =new HelloName(); a.setName(name); return a; } }
使用的bean
package com.diancai.test; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import com.diancai.service.dcrestaurant.WsConstants; @XmlRootElement @XmlType(name="helloname",namespace=WsConstants.NS) public class HelloName { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.diancai.service.dcrestaurant;
/*
* webservice 命名空间
*/
public class WsConstants {
public static final String NS="http://diancai_service.com.diancai.sns";
}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" default-lazy-init="true"> <description>Spring cxf 公共配置</description> <!-- 访问 http://localhost:8082/cxf 查看发布的接口 --> <!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 --> <!-- <context:component-scan base-package="com.diancai"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> --> <!-- <import resource="classpath:META-INF/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf-servlet.xml"/> --> <!-- 访问http://localhost:8082/cxf/soap/testHello?wsdl --> <jaxws:endpoint id="testHelloword" address="/soap/testHello"> <jaxws:implementor ref="testHellowordImpl"/> </jaxws:endpoint> <bean id="testHellowordImpl" class="com.diancai.test.impl.TestHellowordImpl"></bean> <!-- rest --> <!-- 访问http://localhost:8082/cxf/jaxrs/testHellowordRest/say/1 --> <jaxrs:server id="serviceContainer" address="/jaxrs"> <jaxrs:serviceBeans> <ref bean="testHellowordRest"/> </jaxrs:serviceBeans> <jaxrs:providers> <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" /> </jaxrs:providers> <jaxrs:extensionMappings> <entry key="xml" value="application/xml"></entry> </jaxrs:extensionMappings> </jaxrs:server> <bean id="testHellowordRest" class="com.diancai.test.impl.TestHellowordRestImpl"></bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>web service</display-name> <!-- for soap ws --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- for cxf servlet --> <servlet> <servlet-name>cxfServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxfServlet</servlet-name> <url-pattern>/cxf/*</url-pattern> </servlet-mapping> <!-- 如果指明了 contextConfigLocation就不会按照默认配置文件名(上面的servlet.xml) --> <!-- 如果指明了 contextConfigLocation就不会按照默认配置文件名(上面的servlet.xml) --> <!-- 注意xml 文件 节点的顺序问题,有时候可能报错 --> <!-- 不使用listener监听器来加载Spring 的配置,改用DispatcherServlet来加载spring的配置,简单 --> <!-- for spring mvc --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 防止Spring mvc 乱码 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
测试
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- rs --> <bean id="testHelloword" class="org.apache.cxf.jaxrs.client.JAXRSClientFactory" factory-method="create"> <constructor-arg type="java.lang.String" value="http://localhost:8082/cxf/jaxrs/"></constructor-arg> <constructor-arg type="java.lang.Class" value="com.diancai.webserivce.rest.TestHellowordRest"></constructor-arg> </bean> <!-- rs 还有一种方式 (注意org.apache.cxf.jaxrs.client.WebClient, 不是 jaxrsclientFactory ) --> <!-- <bean id="webClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create"> <constructor-arg type="java.lang.String" value="http://localhost:8000/CXFWebService/rest/" /> </bean> --> <!-- soap --> <bean id="restauRantClent" class="com.diancai.webserivce.rest.TestHelloword" factory-bean="clientFactory" factory-method="create"></bean> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.diancai.webserivce.rest.TestHelloword"></property> <property name="address" value="http://localhost:8082/cxf/soap/testHello"></property> </bean> </beans>
package com.diancai.webserivce.rest; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Aa { public static void main(String[] args){ ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); TestHelloword t=app.getBean("restauRantClent",TestHelloword.class); System.out.println(t.getClass().getName()+"fds"); System.out.println(t.sayHi("张三")); } }