一、首先创建一个父亲项目,父亲项目下面有3个儿子模块(webservice)
<?xml version='1.0' encoding='UTF-8' ?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <!--Base --> <modelVersion>4.0.0</modelVersion> <groupId>jmust.ws</groupId> <artifactId>webservice</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <name>Web-Service</name> <url>http://common.yy.com/webservice</url> <distributionManagement> <repository> <id>yy</id> <name>yy Repository</name> <url>http://admin:[email protected]:8080/nexus/content/repositories/yyRepos/</url> <layout>default</layout> </repository> </distributionManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> </plugins> </build> <!-- 依赖关系管理 --> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <type>jar</type> <scope>test</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test-blueprint</artifactId> <version>2.14.2</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.15.2</version> </dependency> </dependencies> </dependencyManagement> <!-- 依赖 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <type>jar</type> <scope>test</scope> <optional>true</optional> </dependency> </dependencies> <!--子模块 --> <modules> <module>ws_inter</module> <module>ws_impl</module> <module>ws_service</module> </modules> </project>
(1)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>jmust.ws</groupId> <artifactId>webservice</artifactId> <version>1.0.0</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>jmust.ws.webservice</groupId> <artifactId>inter</artifactId> <packaging>bundle</packaging> <version>1.0.0</version> <name>ws-inter</name> <url>http://maven.apache.org</url> <distributionManagement> <repository> <id>jmust</id> <name>jmust Repository</name> <url>http://admin:[email protected]:8081/nexus/content/repositories/JMustRepos/</url> <layout>default</layout> </repository> </distributionManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test-blueprint</artifactId> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> </dependencies> <build> <plugins> <!-- to generate the MANIFEST-FILE of the bundle --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.5.4</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-Vendor>yy</Bundle-Vendor> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.groupId}.${project.ArtifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName> <Export-Package>jmust.ws.webservice.inter;version="1.0.0"</Export-Package> <Import-Package> org.osgi.framework,org.slf4j </Import-Package> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>compile</phase> </execution> </executions> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>deploy</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.2</version> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </project>
@WebService public interface Calculator { @WebMethod public String getVersion(String request); @WebMethod public Long add(Integer a, Integer b); @WebMethod public Long subtract(Integer a, Integer b); @WebMethod public Long multiply(Integer a, Integer b); }
(1)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>jmust.ws</groupId> <artifactId>webservice</artifactId> <version>1.0.0</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>jmust.ws.webservice</groupId> <artifactId>impl</artifactId> <packaging>bundle</packaging> <version>1.0.0</version> <name>ws-impl</name> <url>http://maven.apache.org</url> <distributionManagement> <repository> <id>jmust</id> <name>jmust Repository</name> <url>http://admin:[email protected]:8081/nexus/content/repositories/JMustRepos/</url> <layout>default</layout> </repository> </distributionManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.14.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.0.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>jmust.ws.webservice</groupId> <artifactId>inter</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <!-- to generate the MANIFEST-FILE of the bundle --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.5.4</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-Vendor>yy</Bundle-Vendor> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.groupId}.${project.ArtifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName> <Export-Package>jmust.ws.webservice.impl;version="1.0.0"</Export-Package> <Import-Package> jmust.ws.webservice.inter </Import-Package> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>compile</phase> </execution> </executions> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>deploy</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>false</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.2</version> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </project>
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf" xmlns:cxfcore="http://cxf.apache.org/blueprint/core" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd" > <bean id="wsImplBean" class="jmust.ws.webservice.impl.CalculatorImpl"> <!-- <property name="userName" value="lvkun"/> --> </bean> <service id="wsService" ref="wsImplBean" interface="jmust.ws.webservice.inter.Calculator"> </service> </blueprint>
public class CalculatorImpl implements Calculator{ @Override public String getVersion(String request) { // TODO Auto-generated method stub System.out.println(request); return "1.0.0"; } @Override public Long add(Integer a, Integer b) { // TODO Auto-generated method stub System.out.println(a+","+b); return Long.valueOf(a+b); } @Override public Long subtract(Integer a, Integer b) { // TODO Auto-generated method stub System.out.println(a+","+b); return Long.valueOf(a - b); } @Override public Long multiply(Integer a, Integer b) { // TODO Auto-generated method stub System.out.println(a + ", " + b); return Long.valueOf(a * b); } }
(1)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>jmust.ws</groupId> <artifactId>webservice</artifactId> <version>1.0.0</version> <relativePath>../pom.xml</relativePath> </parent> <groupId>jmust.ws.webservice</groupId> <artifactId>service</artifactId> <packaging>bundle</packaging> <version>1.0.0</version> <name>ws-service</name> <url>http://maven.apache.org</url> <distributionManagement> <repository> <id>jmust</id> <name>jmust Repository</name> <url>http://admin:[email protected]:8081/nexus/content/repositories/JMustRepos/</url> <layout>default</layout> </repository> </distributionManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test-blueprint</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.14.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.0.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jetty</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>jmust.ws.webservice</groupId> <artifactId>inter</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <!-- to generate the MANIFEST-FILE of the bundle --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.5.4</version> <extensions>true</extensions> <configuration> <instructions> <Bundle-Vendor>yy</Bundle-Vendor> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.groupId}.${project.ArtifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName> <Export-Package>jmust.ws.webservice.service;version="1.0.0"</Export-Package> <Import-Package> org.osgi.framework,org.slf4j,org.apache.camel,org.apache.cxf.message, jmust.ws.webservice.inter </Import-Package> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>compile</phase> </execution> </executions> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>deploy</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.2</version> <configuration> <skip>false</skip> </configuration> </plugin> </plugins> </build> </project>
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf" xmlns:cxfcore="http://cxf.apache.org/blueprint/core" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd"> <reference id="GWService" interface="jmust.ws.webservice.inter.Calculator" /> <bean id="calcProcessor" class="jmust.ws.webservice.service.CalculatorProcessor"> <property name="gwService" ref="GWService" /> </bean> <camel-cxf:cxfEndpoint id="calcWSEndpoint" serviceClass="jmust.ws.webservice.inter.Calculator" address="http://127.0.0.1:9911/ws/calc" bindingId="http://www.w3.org/2003/05/soap/bindings/HTTP/"> </camel-cxf:cxfEndpoint> <camelContext id="ACamelContext" xmlns="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <!-- 处理web-service请求 方式一--> <route> <from uri="calcWSEndpoint" /> <setHeader headerName="CamelBeanMethodName"> <simple>${in.header.operationName}</simple> </setHeader> <setHeader headerName="CamelBeanMultiParameterArray"> <constant>true</constant> </setHeader> <to uri="bean:GWService" /> </route> <!-- 处理web-service请求 方式二 --> <!-- <route> <from uri="calcWSEndpoint"/> <to uri="bean:calcProcessor"/> </route> --> <!-- 测试 --> <route> <from uri="jetty:http://127.0.0.1:8080/jetty" /> <to uri="log:aa" /> </route> </camelContext> </blueprint>
public class CalculatorProcessor implements Processor{ private static final Logger log = LoggerFactory.getLogger(CalculatorProcessor.class); private Calculator gwService; public Calculator getGwService() { return gwService; } public void setGwService(Calculator gwService) { this.gwService = gwService; } /** * 根据operatioNname 去执行相应的逻辑,然后设置相应的返回值(一定要和接口的返回值一致) */ @Override public void process(Exchange exchange) throws Exception { // TODO Auto-generated method stub MessageContentsList args = exchange.getIn().getBody(MessageContentsList.class); String method = exchange.getIn().getHeader("operatioNname", String.class); if("getVersion".equals(method)) { exchange.getOut().setBody("2.14.1"); return; } Object arg1 = args.get(0); Object arg2 = args.get(1); Integer a = Integer.valueOf(arg1.toString()); Integer b = Integer.valueOf(arg2.toString()); if("add".equals(method)) { exchange.getOut().setBody(gwService.add(a, b)); } else if("subtract".equals(method)) { exchange.getOut().setBody(gwService.subtract(a, b)); } else if("multiply".equals(method)) { exchange.getOut().setBody(gwService.multiply(a, b)); } } }