环境:JBoss Fuse 6.2.0
1. 学习重点
(1)开发 JAX-WS Web Service。
(2)使用 CXF 的 JaxWsProxyFactoryBean 创建 Client 端 proxy,调用远程 Web Service。
2. blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
JBoss, Home of Professional Open Source
Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
contributors by the @authors tag. See the copyright.txt in the
distribution for a full listing of individual contributors.
Licensed 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.
-->
<!--
This is the OSGi Blueprint XML file defining the CXF JAX-WS beans. Because the file is in the
OSGI-INF/blueprint directory inside our JAR, it will be automatically activated as soon as the artifact is installed.
The root element for any OSGi Blueprint file is 'blueprint' - you also see the namespace definitions for the Blueprint
and the CXF Core and JAX-WS namespaces.
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
xmlns:cxf="http://cxf.apache.org/blueprint/core"
xsi:schemaLocation="
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd">
<!--
Adding a <cxf:bus/> definition to the Blueprint XML file is optional: it is only required if you want to specify
some additional configuration for the CXF Bus instance used by the service endpoints defined in this XML file.
-->
<cxf:bus>
<!--
In this example, we're enabling the logging feature. This will ensure that both the inbound and outbound
XML message are being logged for every web service invocation.
-->
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus>
<!--
Finally, we're configuring the actual JAX-WS endpoint, referring to our web service implementation class and the URI
address we want to assign to our service. The address is relative to the CXF servlet URI, with the default configuration
in place, this endpoint will be available at 'http://localhost:8181/cxf/HelloWorld'.
-->
<jaxws:endpoint id="helloWorld"
implementor="io.fabric8.quickstarts.soap.HelloWorldImpl"
address="/HelloWorld">
</jaxws:endpoint>
</blueprint>
3. HelloWorldImpl.java
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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.
*/
// START SNIPPET: service
package io.fabric8.quickstarts.soap;
import javax.jws.WebService;
/**
* This is our web service implementation, which implements the web service interface.
* We also add the @WebService annotation to it to mark this class an implementation for the endpoint interface.
*/
@WebService(endpointInterface = "io.fabric8.quickstarts.soap.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
/**
* Just a simple implementation for a friendly message that says hello.
*/
public String sayHi(String name) {
return "Hello " + name;
}
}
4. SoapTest.java
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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 io.fabric8.quickstarts.soap;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* A Java client application that uses a plain HTTP URL connection to send a SOAP request and afterwards receive the
* SOAP response.
*/
public class SoapTest {
private static final Logger LOG = LoggerFactory.getLogger(SoapTest.class);
/**
* Helper method to copy bytes from an InputStream to an OutputStream.
*/
private static void copyInputStream(InputStream in, OutputStream out) throws Exception {
int c = 0;
try {
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
in.close();
}
}
/**
* Helper method to read bytes from an InputStream and return them as a String.
*/
private static String getStringFromInputStream(InputStream in) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
copyInputStream(in, bos);
bos.close();
return bos.toString();
}
@Test
public void sendRequest() throws Exception {
String res;
/*
* Set up the URL connection to the web service address
*/
URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorld").openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
/*
* We have prepared a SOAP request in an XML file, so we send the contents of that file to our web service...
*/
OutputStream os = connection.getOutputStream();
InputStream fis = SoapTest.class.getResourceAsStream("/request.xml");
copyInputStream(fis, os);
/*
* ... and afterwards, we just read the SOAP response message that is sent back by the server.
*/
InputStream is = connection.getInputStream();
LOG.info("the response is ====> ");
res = getStringFromInputStream(is);
LOG.info(res);
Assert.assertTrue(res.contains("Hello"));
}
}
5. 编译、部署、卸载
(1)cd /Users/maping/Redhat/fuse/jboss-fuse-6.2.0.redhat-133/quickstarts/cxf/soap
(2)mvn clean install
(3)./fuse
(4)osgi:install -s mvn:org.jboss.quickstarts.fuse/cxf-soap/6.2.0.redhat-133
(5)osgi:list
(6)http://localhost:8181/cxf/
(7)http://localhost:8181/cxf/HelloWorld?wsdl
(8)cxf:list-endpoints
(9)mvn -Ptest
(10)osgi:uninstall <id>
6. 使用 HttpRequester 测试
7. 修改 /cxf servlet 别名
默认情况下,CXF Servlet 的默认使用 '/cxf' 别名。如果要修改,可以
(1)增加 org.apache.cxf.osgi.cfg 文件到 /etc 目录下,并且增加 org.apache.cxf.servlet.context属性,比如:org.apache.cxf.servlet.context=/custom
(2)使用 shell 命令行,比如
config:edit org.apache.cxf.osgi
config:propset org.apache.cxf.servlet.context /custom
config:update
1. 学习重点
(1)开发 JAX-WS Web Service。
(2)使用 CXF 的 JaxWsProxyFactoryBean 创建 Client 端 proxy,调用远程 Web Service。
2. blueprint.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
JBoss, Home of Professional Open Source
Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
contributors by the @authors tag. See the copyright.txt in the
distribution for a full listing of individual contributors.
Licensed 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.
-->
<!--
This is the OSGi Blueprint XML file defining the CXF JAX-WS beans. Because the file is in the
OSGI-INF/blueprint directory inside our JAR, it will be automatically activated as soon as the artifact is installed.
The root element for any OSGi Blueprint file is 'blueprint' - you also see the namespace definitions for the Blueprint
and the CXF Core and JAX-WS namespaces.
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
xmlns:cxf="http://cxf.apache.org/blueprint/core"
xsi:schemaLocation="
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd">
<!--
Adding a <cxf:bus/> definition to the Blueprint XML file is optional: it is only required if you want to specify
some additional configuration for the CXF Bus instance used by the service endpoints defined in this XML file.
-->
<cxf:bus>
<!--
In this example, we're enabling the logging feature. This will ensure that both the inbound and outbound
XML message are being logged for every web service invocation.
-->
<cxf:features>
<cxf:logging/>
</cxf:features>
</cxf:bus>
<!--
Finally, we're configuring the actual JAX-WS endpoint, referring to our web service implementation class and the URI
address we want to assign to our service. The address is relative to the CXF servlet URI, with the default configuration
in place, this endpoint will be available at 'http://localhost:8181/cxf/HelloWorld'.
-->
<jaxws:endpoint id="helloWorld"
implementor="io.fabric8.quickstarts.soap.HelloWorldImpl"
address="/HelloWorld">
</jaxws:endpoint>
</blueprint>
3. HelloWorldImpl.java
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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.
*/
// START SNIPPET: service
package io.fabric8.quickstarts.soap;
import javax.jws.WebService;
/**
* This is our web service implementation, which implements the web service interface.
* We also add the @WebService annotation to it to mark this class an implementation for the endpoint interface.
*/
@WebService(endpointInterface = "io.fabric8.quickstarts.soap.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
/**
* Just a simple implementation for a friendly message that says hello.
*/
public String sayHi(String name) {
return "Hello " + name;
}
}
4. SoapTest.java
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed 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 io.fabric8.quickstarts.soap;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* A Java client application that uses a plain HTTP URL connection to send a SOAP request and afterwards receive the
* SOAP response.
*/
public class SoapTest {
private static final Logger LOG = LoggerFactory.getLogger(SoapTest.class);
/**
* Helper method to copy bytes from an InputStream to an OutputStream.
*/
private static void copyInputStream(InputStream in, OutputStream out) throws Exception {
int c = 0;
try {
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
in.close();
}
}
/**
* Helper method to read bytes from an InputStream and return them as a String.
*/
private static String getStringFromInputStream(InputStream in) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
copyInputStream(in, bos);
bos.close();
return bos.toString();
}
@Test
public void sendRequest() throws Exception {
String res;
/*
* Set up the URL connection to the web service address
*/
URLConnection connection = new URL("http://localhost:8181/cxf/HelloWorld").openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
/*
* We have prepared a SOAP request in an XML file, so we send the contents of that file to our web service...
*/
OutputStream os = connection.getOutputStream();
InputStream fis = SoapTest.class.getResourceAsStream("/request.xml");
copyInputStream(fis, os);
/*
* ... and afterwards, we just read the SOAP response message that is sent back by the server.
*/
InputStream is = connection.getInputStream();
LOG.info("the response is ====> ");
res = getStringFromInputStream(is);
LOG.info(res);
Assert.assertTrue(res.contains("Hello"));
}
}
5. 编译、部署、卸载
(1)cd /Users/maping/Redhat/fuse/jboss-fuse-6.2.0.redhat-133/quickstarts/cxf/soap
(2)mvn clean install
(3)./fuse
(4)osgi:install -s mvn:org.jboss.quickstarts.fuse/cxf-soap/6.2.0.redhat-133
(5)osgi:list
(6)http://localhost:8181/cxf/
(7)http://localhost:8181/cxf/HelloWorld?wsdl
(8)cxf:list-endpoints
(9)mvn -Ptest
(10)osgi:uninstall <id>
6. 使用 HttpRequester 测试
7. 修改 /cxf servlet 别名
默认情况下,CXF Servlet 的默认使用 '/cxf' 别名。如果要修改,可以
(1)增加 org.apache.cxf.osgi.cfg 文件到 /etc 目录下,并且增加 org.apache.cxf.servlet.context属性,比如:org.apache.cxf.servlet.context=/custom
(2)使用 shell 命令行,比如
config:edit org.apache.cxf.osgi
config:propset org.apache.cxf.servlet.context /custom
config:update
没有评论:
发表评论