CXF 实现 webservice 并且部署在web项目中 tomcat作为容器

在tomcat作为容器发布webservice服务前,我们先来看一个简单的不通过容器即可发布服务的例子

package com.tree.webservice;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

	public String sayHello(String content);
	
}

package com.tree.webservice.impl;

import javax.jws.WebService;

import com.tree.webservice.HelloWorld;

@WebService(endpointInterface="com.tree.webservice.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String content) {
		// TODO Auto-generated method stub
		return "Hello "+content;
	}

}

package com.tree.webservice.publish;

import javax.xml.ws.Endpoint;

import com.tree.webservice.HelloWorld;
import com.tree.webservice.impl.HelloWorldImpl;

public class PublishService {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Service is begin ...");
		HelloWorld hw = new HelloWorldImpl();
		Endpoint.publish("http://localhost:8080/hw", hw);
		System.out.println("Server is OK ...");
	}

}


下面再来看如何调用该发布的服务

package com.tree.webservice.publish;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.tree.webservice.HelloWorld;

public class CallWs {

	public static void main(String args[]) {
		System.out.println("Begin to call the service ...");
		JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();  
        factoryBean.setServiceClass(HelloWorld.class);  
        factoryBean.setAddress("http://localhost:8080/hw");  
          
        HelloWorld hwService = (HelloWorld)factoryBean.create();  
        String result = hwService.sayHello(" chiweitree");
        System.out.println(result); 
	}
	
}

控制台输出

Begin to call the service ...
2014-09-16 09:20:00,358 - org.apache.cxf.common.logging.LogUtils -0    [main] DEBUG  - Using org.apache.cxf.common.logging.Log4jLogger for logging.
2014-09-16 09:20:00,842 - org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean -484  [main] INFO   - Creating Service {http://webservice.tree.com/}HelloWorldService from class com.tree.webservice.HelloWorld
2014-09-16 09:20:01,418 - org.apache.cxf.jaxb.JAXBDataBinding -1060 [main] DEBUG  - Created JAXBContext "jar:file:/home/hadoop/mydisk/maven_repo/com/sun/xml/bind/jaxb-impl/2.2.7/jaxb-impl-2.2.7.jar!/com/sun/xml/bind/v2/runtime/JAXBContextImpl.class Build-Id: 2.2.7
Classes known to this context:
  [B
  boolean
  byte
  char
  com.sun.xml.bind.api.CompositeStructure
  com.tree.webservice.jaxws_asm.SayHello
  com.tree.webservice.jaxws_asm.SayHelloResponse
  double
  float
  int
  java.awt.Image
  java.io.File
  java.lang.Boolean
  java.lang.Byte
  java.lang.Character
  java.lang.Class
  java.lang.Double
  java.lang.Float
  java.lang.Integer
  java.lang.Long
  java.lang.Object
  java.lang.Short
  java.lang.String
  java.lang.Void
  java.math.BigDecimal
  java.math.BigInteger
  java.net.URI
  java.net.URL
  java.util.Calendar
  java.util.Date
  java.util.GregorianCalendar
  java.util.UUID
  javax.activation.DataHandler
  javax.xml.bind.JAXBElement
  javax.xml.datatype.Duration
  javax.xml.datatype.XMLGregorianCalendar
  javax.xml.namespace.QName
  javax.xml.transform.Source
  long
  short
  void
" with classes [class com.tree.webservice.jaxws_asm.SayHello, class com.tree.webservice.jaxws_asm.SayHelloResponse].
2014-09-16 09:20:01,790 - org.apache.cxf.resource.DefaultResourceManager -1432 [main] DEBUG  - resolving resource <org.apache.cxf.wsdl11.WSDLManagerImpl/bus> type <interface org.apache.cxf.Bus>
2014-09-16 09:20:01,790 - org.apache.cxf.resource.DefaultResourceManager -1432 [main] DEBUG  - resolving resource <null> type <interface org.apache.cxf.Bus>
2014-09-16 09:20:02,115 - org.apache.cxf.jaxws.handler.AnnotationHandlerChainBuilder -1757 [main] DEBUG  - building handler chain
2014-09-16 09:20:02,115 - org.apache.cxf.jaxws.handler.AnnotationHandlerChainBuilder -1757 [main] DEBUG  - Checking for HandlerChain annotation on com.tree.webservice.HelloWorld
2014-09-16 09:20:02,115 - org.apache.cxf.jaxws.handler.AnnotationHandlerChainBuilder -1757 [main] DEBUG  - no HandlerChain annotation on interface com.tree.webservice.HelloWorld
2014-09-16 09:20:02,131 - org.apache.cxf.endpoint.ClientImpl -1773 [main] DEBUG  - Invoke, operation info: [BindingOperationInfo: {http://webservice.tree.com/}sayHello], params: [ chiweitree]
2014-09-16 09:20:02,133 - org.apache.cxf.endpoint.ClientImpl -1775 [main] DEBUG  - set requestContext to message be{java.lang.reflect.Method=public abstract java.lang.String com.tree.webservice.HelloWorld.sayHello(java.lang.String), org.apache.cxf.jaxws.context.WrappedMessageContext.SCOPES={org.apache.cxf.message.Message.ENDPOINT_ADDRESS=APPLICATION}, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:8080/hw}
2014-09-16 09:20:02,137 - org.apache.cxf.endpoint.ClientImpl -1779 [main] DEBUG  - Interceptors contributed by bus: [org.apache.cxf.ws.policy.PolicyOutInterceptor@6c313657]
2014-09-16 09:20:02,141 - org.apache.cxf.endpoint.ClientImpl -1783 [main] DEBUG  - Interceptors contributed by client: []
2014-09-16 09:20:02,141 - org.apache.cxf.endpoint.ClientImpl -1783 [main] DEBUG  - Interceptors contributed by endpoint: [org.apache.cxf.interceptor.MessageSenderInterceptor@12e6c13f, org.apache.cxf.jaxws.interceptors.SwAOutInterceptor@6b9918ca, [email protected]acf6, org.apache.cxf.jaxws.interceptors.HolderOutInterceptor@61c152c4]
2014-09-16 09:20:02,141 - org.apache.cxf.endpoint.ClientImpl -1783 [main] DEBUG  - Interceptors contributed by binding: [org.apache.cxf.interceptor.AttachmentOutInterceptor@5b20f3ff, org.apache.cxf.interceptor.StaxOutInterceptor@50731916, org.apache.cxf.binding.soap.interceptor.SoapHeaderOutFilterInterceptor@34741c9d, org.apache.cxf.wsdl.interceptors.WrappedOutInterceptor@3302a252, org.apache.cxf.wsdl.interceptors.BareOutInterceptor@71eecfa7, org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor@75831760, [email protected]e5]
2014-09-16 09:20:02,141 - org.apache.cxf.endpoint.ClientImpl -1783 [main] DEBUG  - Interceptors contributed by databinding: []
2014-09-16 09:20:02,152 - org.apache.cxf.phase.PhaseInterceptorChain -1794 [main] DEBUG  - Adding interceptor org.apache.cxf.ws.policy.PolicyOutInterceptor@6c313657 to phase setup
2014-09-16 09:20:02,159 - org.apache.cxf.phase.PhaseInterceptorChain -1801 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.MessageSenderInterceptor@12e6c13f to phase prepare-send
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.jaxws.interceptors.SwAOutInterceptor@6b9918ca to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected]acf6 to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.jaxws.interceptors.HolderOutInterceptor@61c152c4 to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.AttachmentOutInterceptor@5b20f3ff to phase pre-stream
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.StaxOutInterceptor@50731916 to phase pre-stream
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.binding.soap.interceptor.SoapHeaderOutFilterInterceptor@34741c9d to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.wsdl.interceptors.WrappedOutInterceptor@3302a252 to phase marshal
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.wsdl.interceptors.BareOutInterceptor@71eecfa7 to phase marshal
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor@75831760 to phase post-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected]e5 to phase write
2014-09-16 09:20:02,317 - org.apache.cxf.transport.http.HTTPConduit -1959 [main] DEBUG  - Conduit '{http://webservice.tree.com/}HelloWorldPort.http-conduit' has been (re)configured for plain http.
2014-09-16 09:20:02,323 - org.apache.cxf.transport.http.HTTPConduit -1965 [main] DEBUG  - No Trust Decider configured for Conduit '{http://webservice.tree.com/}HelloWorldPort.http-conduit'
2014-09-16 09:20:02,323 - org.apache.cxf.transport.http.HTTPConduit -1965 [main] DEBUG  - No Auth Supplier configured for Conduit '{http://webservice.tree.com/}HelloWorldPort.http-conduit'
2014-09-16 09:20:02,323 - org.apache.cxf.transport.http.HTTPConduit -1965 [main] DEBUG  - Conduit '{http://webservice.tree.com/}HelloWorldPort.http-conduit' has been configured for plain http.
2014-09-16 09:20:02,323 - org.apache.cxf.transport.http.HTTPConduit -1965 [main] DEBUG  - registering incoming observer: org.apache.cxf.endpoint.ClientImpl@76e68f59
2014-09-16 09:20:02,324 - org.apache.cxf.phase.PhaseInterceptorChain -1966 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@6c5094ec was created. Current flow:
  setup [PolicyOutInterceptor]
  pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
  post-logical [SoapPreProtocolOutInterceptor]
  prepare-send [MessageSenderInterceptor]
  pre-stream [AttachmentOutInterceptor, StaxOutInterceptor]
  write [SoapOutInterceptor]
  marshal [WrappedOutInterceptor, BareOutInterceptor]

2014-09-16 09:20:02,324 - org.apache.cxf.phase.PhaseInterceptorChain -1966 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.ws.policy.PolicyOutInterceptor@6c313657
2014-09-16 09:20:02,325 - org.apache.cxf.phase.PhaseInterceptorChain -1967 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.HolderOutInterceptor@61c152c4
2014-09-16 09:20:02,327 - org.apache.cxf.jaxws.interceptors.HolderOutInterceptor -1969 [main] DEBUG  - op: [OperationInfo: {http://webservice.tree.com/}sayHello]
2014-09-16 09:20:02,328 - org.apache.cxf.jaxws.interceptors.HolderOutInterceptor -1970 [main] DEBUG  - op.hasOutput(): true
2014-09-16 09:20:02,328 - org.apache.cxf.jaxws.interceptors.HolderOutInterceptor -1970 [main] DEBUG  - op.getOutput().size(): 1
2014-09-16 09:20:02,328 - org.apache.cxf.phase.PhaseInterceptorChain -1970 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.SwAOutInterceptor@6b9918ca
2014-09-16 09:20:02,329 - org.apache.cxf.phase.PhaseInterceptorChain -1971 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]acf6
2014-09-16 09:20:02,346 - org.apache.cxf.phase.PhaseInterceptorChain -1988 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.binding.soap.interceptor.SoapHeaderOutFilterInterceptor@34741c9d
2014-09-16 09:20:02,351 - org.apache.cxf.phase.PhaseInterceptorChain -1993 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor@75831760
2014-09-16 09:20:02,351 - org.apache.cxf.phase.PhaseInterceptorChain -1993 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.MessageSenderInterceptor@12e6c13f
2014-09-16 09:20:02,395 - org.apache.cxf.phase.PhaseInterceptorChain -2037 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor@c63679a to phase prepare-send-ending
2014-09-16 09:20:02,411 - org.apache.cxf.phase.PhaseInterceptorChain -2053 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@6c5094ec was modified. Current flow:
  setup [PolicyOutInterceptor]
  pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
  post-logical [SoapPreProtocolOutInterceptor]
  prepare-send [MessageSenderInterceptor]
  pre-stream [AttachmentOutInterceptor, StaxOutInterceptor]
  write [SoapOutInterceptor]
  marshal [WrappedOutInterceptor, BareOutInterceptor]
  prepare-send-ending [MessageSenderEndingInterceptor]

2014-09-16 09:20:02,411 - org.apache.cxf.phase.PhaseInterceptorChain -2053 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.AttachmentOutInterceptor@5b20f3ff
2014-09-16 09:20:02,432 - org.apache.cxf.phase.PhaseInterceptorChain -2074 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.StaxOutInterceptor@50731916
2014-09-16 09:20:02,548 - org.apache.cxf.phase.PhaseInterceptorChain -2190 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.StaxOutEndingInterceptor@464232fb to phase pre-stream-ending
2014-09-16 09:20:02,549 - org.apache.cxf.phase.PhaseInterceptorChain -2191 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@6c5094ec was modified. Current flow:
  setup [PolicyOutInterceptor]
  pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
  post-logical [SoapPreProtocolOutInterceptor]
  prepare-send [MessageSenderInterceptor]
  pre-stream [AttachmentOutInterceptor, StaxOutInterceptor]
  write [SoapOutInterceptor]
  marshal [WrappedOutInterceptor, BareOutInterceptor]
  pre-stream-ending [StaxOutEndingInterceptor]
  prepare-send-ending [MessageSenderEndingInterceptor]

2014-09-16 09:20:02,549 - org.apache.cxf.phase.PhaseInterceptorChain -2191 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]e5
2014-09-16 09:20:02,552 - org.apache.cxf.phase.PhaseInterceptorChain -2194 [main] DEBUG  - Adding interceptor org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor@27763e5f to phase write-ending
2014-09-16 09:20:02,559 - org.apache.cxf.phase.PhaseInterceptorChain -2201 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@6c5094ec was modified. Current flow:
  setup [PolicyOutInterceptor]
  pre-logical [HolderOutInterceptor, SwAOutInterceptor, WrapperClassOutInterceptor, SoapHeaderOutFilterInterceptor]
  post-logical [SoapPreProtocolOutInterceptor]
  prepare-send [MessageSenderInterceptor]
  pre-stream [AttachmentOutInterceptor, StaxOutInterceptor]
  write [SoapOutInterceptor]
  marshal [WrappedOutInterceptor, BareOutInterceptor]
  write-ending [SoapOutEndingInterceptor]
  pre-stream-ending [StaxOutEndingInterceptor]
  prepare-send-ending [MessageSenderEndingInterceptor]

2014-09-16 09:20:02,562 - org.apache.cxf.phase.PhaseInterceptorChain -2204 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.wsdl.interceptors.WrappedOutInterceptor@3302a252
2014-09-16 09:20:02,562 - org.apache.cxf.phase.PhaseInterceptorChain -2204 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.wsdl.interceptors.BareOutInterceptor@71eecfa7
2014-09-16 09:20:02,584 - org.apache.cxf.phase.PhaseInterceptorChain -2226 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor$SoapOutEndingInterceptor@27763e5f
2014-09-16 09:20:02,585 - org.apache.cxf.phase.PhaseInterceptorChain -2227 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.StaxOutEndingInterceptor@464232fb
2014-09-16 09:20:02,589 - org.apache.cxf.phase.PhaseInterceptorChain -2231 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor@c63679a
2014-09-16 09:20:02,590 - org.apache.cxf.transport.http.Headers -2232 [main] DEBUG  - Accept: */*
2014-09-16 09:20:02,590 - org.apache.cxf.transport.http.Headers -2232 [main] DEBUG  - SOAPAction: ""
2014-09-16 09:20:02,590 - org.apache.cxf.transport.http.HTTPConduit -2232 [main] DEBUG  - No Trust Decider for Conduit '{http://webservice.tree.com/}HelloWorldPort.http-conduit'. An afirmative Trust Decision is assumed.
2014-09-16 09:20:02,650 - org.apache.cxf.transport.http.HTTPConduit -2292 [main] DEBUG  - Sending POST Message with Headers to http://localhost:8080/hw Conduit :{http://webservice.tree.com/}HelloWorldPort.http-conduit

2014-09-16 09:20:02,768 - org.apache.cxf.endpoint.ClientImpl -2410 [main] DEBUG  - Interceptors contributed by bus: [org.apache.cxf.ws.policy.PolicyInInterceptor@507a2840]
2014-09-16 09:20:02,774 - org.apache.cxf.endpoint.ClientImpl -2416 [main] DEBUG  - Interceptors contributed by client: []
2014-09-16 09:20:02,776 - org.apache.cxf.endpoint.ClientImpl -2418 [main] DEBUG  - Interceptors contributed by endpoint: [[email protected]07, org.apache.cxf.jaxws.interceptors.HolderInInterceptor@20345a92, org.apache.cxf.jaxws.interceptors.SwAInInterceptor@4d2637c2, org.apache.cxf.frontend.WSDLGetInterceptor@816c920]
2014-09-16 09:20:02,777 - org.apache.cxf.endpoint.ClientImpl -2419 [main] DEBUG  - Interceptors contributed by binding: [org.apache.cxf.interceptor.AttachmentInInterceptor@fee5806, org.apache.cxf.interceptor.StaxInInterceptor@68d4fa15, [email protected]66eae77, [email protected], [email protected]7416a, [email protected]97cbe3, [email protected]de58, [email protected]7647, org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor@7bc230bf]
2014-09-16 09:20:02,777 - org.apache.cxf.endpoint.ClientImpl -2419 [main] DEBUG  - Interceptors contributed by databinging: [org.apache.cxf.jaxb.attachment.JAXBAttachmentSchemaValidationHack@a0e8b13]
2014-09-16 09:20:02,777 - org.apache.cxf.phase.PhaseInterceptorChain -2419 [main] DEBUG  - Adding interceptor org.apache.cxf.ws.policy.PolicyInInterceptor@507a2840 to phase receive
2014-09-16 09:20:02,778 - org.apache.cxf.phase.PhaseInterceptorChain -2420 [main] DEBUG  - Adding interceptor [email protected]07 to phase post-logical
2014-09-16 09:20:02,778 - org.apache.cxf.phase.PhaseInterceptorChain -2420 [main] DEBUG  - Adding interceptor org.apache.cxf.jaxws.interceptors.HolderInInterceptor@20345a92 to phase pre-invoke
2014-09-16 09:20:02,778 - org.apache.cxf.phase.PhaseInterceptorChain -2420 [main] DEBUG  - Adding interceptor org.apache.cxf.jaxws.interceptors.SwAInInterceptor@4d2637c2 to phase pre-invoke
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor org.apache.cxf.frontend.WSDLGetInterceptor@816c920 to phase read
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.AttachmentInInterceptor@fee5806 to phase receive
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.StaxInInterceptor@68d4fa15 to phase post-stream
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected]66eae77 to phase read
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected] to phase unmarshal
2014-09-16 09:20:02,780 - org.apache.cxf.phase.PhaseInterceptorChain -2422 [main] DEBUG  - Adding interceptor [email protected]7416a to phase unmarshal
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor [email protected]97cbe3 to phase read
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor [email protected]de58 to phase read
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor [email protected]7647 to phase post-protocol
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor@7bc230bf to phase pre-protocol
2014-09-16 09:20:02,783 - org.apache.cxf.phase.PhaseInterceptorChain -2425 [main] DEBUG  - Adding interceptor org.apache.cxf.jaxb.attachment.JAXBAttachmentSchemaValidationHack@a0e8b13 to phase post-protocol
2014-09-16 09:20:02,783 - org.apache.cxf.phase.PhaseInterceptorChain -2425 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@40cc918d was created. Current flow:
  receive [PolicyInInterceptor, AttachmentInInterceptor]
  post-stream [StaxInInterceptor]
  read [WSDLGetInterceptor, ReadHeadersInterceptor, SoapActionInInterceptor, StartBodyInterceptor]
  pre-protocol [MustUnderstandInterceptor]
  post-protocol [CheckFaultInterceptor, JAXBAttachmentSchemaValidationHack]
  unmarshal [DocLiteralInInterceptor, SoapHeaderInterceptor]
  post-logical [WrapperClassInInterceptor]
  pre-invoke [SwAInInterceptor, HolderInInterceptor]

2014-09-16 09:20:02,783 - org.apache.cxf.phase.PhaseInterceptorChain -2425 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.ws.policy.PolicyInInterceptor@507a2840
2014-09-16 09:20:02,807 - org.apache.cxf.phase.PhaseInterceptorChain -2449 [main] DEBUG  - Adding interceptor org.apache.cxf.ws.policy.PolicyVerificationInInterceptor@5d62b0c to phase pre-invoke
2014-09-16 09:20:02,808 - org.apache.cxf.phase.PhaseInterceptorChain -2450 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@40cc918d was modified. Current flow:
  receive [PolicyInInterceptor, AttachmentInInterceptor]
  post-stream [StaxInInterceptor]
  read [WSDLGetInterceptor, ReadHeadersInterceptor, SoapActionInInterceptor, StartBodyInterceptor]
  pre-protocol [MustUnderstandInterceptor]
  post-protocol [CheckFaultInterceptor, JAXBAttachmentSchemaValidationHack]
  unmarshal [DocLiteralInInterceptor, SoapHeaderInterceptor]
  post-logical [WrapperClassInInterceptor]
  pre-invoke [SwAInInterceptor, HolderInInterceptor, PolicyVerificationInInterceptor]

2014-09-16 09:20:02,808 - org.apache.cxf.phase.PhaseInterceptorChain -2450 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.AttachmentInInterceptor@fee5806
2014-09-16 09:20:02,808 - org.apache.cxf.phase.PhaseInterceptorChain -2450 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.StaxInInterceptor@68d4fa15
2014-09-16 09:20:02,867 - org.apache.cxf.phase.PhaseInterceptorChain -2509 [main] DEBUG  - Adding interceptor org.apache.cxf.interceptor.StaxInEndingInterceptor@67c6cf7f to phase pre-invoke
2014-09-16 09:20:02,877 - org.apache.cxf.phase.PhaseInterceptorChain -2519 [main] DEBUG  - Chain org.apache.cxf.phase.PhaseInterceptorChain@40cc918d was modified. Current flow:
  receive [PolicyInInterceptor, AttachmentInInterceptor]
  post-stream [StaxInInterceptor]
  read [WSDLGetInterceptor, ReadHeadersInterceptor, SoapActionInInterceptor, StartBodyInterceptor]
  pre-protocol [MustUnderstandInterceptor]
  post-protocol [CheckFaultInterceptor, JAXBAttachmentSchemaValidationHack]
  unmarshal [DocLiteralInInterceptor, SoapHeaderInterceptor]
  post-logical [WrapperClassInInterceptor]
  pre-invoke [StaxInEndingInterceptor, SwAInInterceptor, HolderInInterceptor, PolicyVerificationInInterceptor]

2014-09-16 09:20:02,878 - org.apache.cxf.phase.PhaseInterceptorChain -2520 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.frontend.WSDLGetInterceptor@816c920
2014-09-16 09:20:02,879 - org.apache.cxf.phase.PhaseInterceptorChain -2521 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]97cbe3
2014-09-16 09:20:02,888 - org.apache.cxf.phase.PhaseInterceptorChain -2530 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]66eae77
2014-09-16 09:20:02,892 - org.apache.cxf.phase.PhaseInterceptorChain -2534 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]de58
2014-09-16 09:20:02,892 - org.apache.cxf.phase.PhaseInterceptorChain -2534 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor@7bc230bf
2014-09-16 09:20:02,893 - org.apache.cxf.phase.PhaseInterceptorChain -2535 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]7647
2014-09-16 09:20:02,893 - org.apache.cxf.phase.PhaseInterceptorChain -2535 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.jaxb.attachment.JAXBAttachmentSchemaValidationHack@a0e8b13
2014-09-16 09:20:02,894 - org.apache.cxf.phase.PhaseInterceptorChain -2536 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,943 - org.apache.cxf.phase.PhaseInterceptorChain -2585 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]7416a
2014-09-16 09:20:02,945 - org.apache.cxf.phase.PhaseInterceptorChain -2587 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]07
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.StaxInEndingInterceptor@67c6cf7f
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.SwAInInterceptor@4d2637c2
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.jaxws.interceptors.HolderInInterceptor@20345a92
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.ws.policy.PolicyVerificationInInterceptor@5d62b0c
2014-09-16 09:20:02,960 - org.apache.cxf.ws.policy.PolicyVerificationInInterceptor -2602 [main] DEBUG  - Verified policies for inbound message.
Hello  chiweitree


但是其实在实际项目中,并不希望webservice和应用是分开的,希望它们在同一个容器中,比如tomcat,这样我们就通过WEB来部署webservice服务了

首先来配置web.xml,定义CXFServlet等等

<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Archetype Created Web Application</display-name>
	<servlet>    
        <servlet-name>CXFService</servlet-name>    
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
    </servlet>    
    
    <servlet-mapping>    
        <servlet-name>CXFService</servlet-name>    
        <url-pattern>/ws/*</url-pattern>    
    </servlet-mapping>  

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring/spring-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
</web-app>
在来定义spring-ws.xml

<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jaxws="http://cxf.apache.org/jaxws"   
    xsi:schemaLocation=" http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd   
        http://cxf.apache.org/jaxws   
        http://cxf.apache.org/schemas/jaxws.xsd">  
        
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    
    <bean id="helloWorldImpl" class="com.tree.webservice.impl.HelloWorldImpl"/>
    
	<jaxws:endpoint id="heloWorld" implementor="#helloWorldImpl" address="/hw">
		<jaxws:properties>
			<entry key="schema-validate-enabled" value="true"/>
		</jaxws:properties>
	</jaxws:endpoint>    
          
</beans>  
启动tomcat服务器

这时候注意访问地址

ip:port/项目名/url-pattern in web.xml/address?wsdl

http://localhost:8080/demo.web/ws/hw?wsdl

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservice.tree.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://webservice.tree.com/" name="HelloWorldImplService" targetNamespace="http://impl.webservice.tree.com/">
<wsdl:import location="http://localhost:8080/demo.web/ws/hw?wsdl=HelloWorld.wsdl" namespace="http://webservice.tree.com/"></wsdl:import>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/demo.web/ws/hw"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


发布成功

同样用前面的调用实例调用一次试试即可。







































































































郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。