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: [[email protected]]
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: [[email protected], [email protected], [email protected]aacf6, [email protected]]
2014-09-16 09:20:02,141 - org.apache.cxf.endpoint.ClientImpl -1783 [main] DEBUG  - Interceptors contributed by binding: [[email protected], [email protected], org.apa[email protected]34741c9d, [email protected], [email protected], org.ap[email protected]75831760, [email protected]be5]
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 [email protected] to phase setup
2014-09-16 09:20:02,159 - org.apache.cxf.phase.PhaseInterceptorChain -1801 [main] DEBUG  - Adding interceptor [email protected] to phase prepare-send
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected] to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected]aacf6 to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected] to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected] to phase pre-stream
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected] to phase pre-stream
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.apa[email protected]34741c9d to phase pre-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected] to phase marshal
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected] to phase marshal
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor org.ap[email protected]75831760 to phase post-logical
2014-09-16 09:20:02,160 - org.apache.cxf.phase.PhaseInterceptorChain -1802 [main] DEBUG  - Adding interceptor [email protected]be5 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: [email protected]
2014-09-16 09:20:02,324 - org.apache.cxf.phase.PhaseInterceptorChain -1966 [main] DEBUG  - Chain [email protected] 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 [email protected]
2014-09-16 09:20:02,325 - org.apache.cxf.phase.PhaseInterceptorChain -1967 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
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 [email protected]
2014-09-16 09:20:02,329 - org.apache.cxf.phase.PhaseInterceptorChain -1971 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]aacf6
2014-09-16 09:20:02,346 - org.apache.cxf.phase.PhaseInterceptorChain -1988 [main] DEBUG  - Invoking handleMessage on interceptor org.apa[email protected]34741c9d
2014-09-16 09:20:02,351 - org.apache.cxf.phase.PhaseInterceptorChain -1993 [main] DEBUG  - Invoking handleMessage on interceptor org.ap[email protected]75831760
2014-09-16 09:20:02,351 - org.apache.cxf.phase.PhaseInterceptorChain -1993 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,395 - org.apache.cxf.phase.PhaseInterceptorChain -2037 [main] DEBUG  - Adding interceptor org.apache.cxf.inte[email protected]c63679a to phase prepare-send-ending
2014-09-16 09:20:02,411 - org.apache.cxf.phase.PhaseInterceptorChain -2053 [main] DEBUG  - Chain [email protected] 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 [email protected]
2014-09-16 09:20:02,432 - org.apache.cxf.phase.PhaseInterceptorChain -2074 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,548 - org.apache.cxf.phase.PhaseInterceptorChain -2190 [main] DEBUG  - Adding interceptor [email protected] to phase pre-stream-ending
2014-09-16 09:20:02,549 - org.apache.cxf.phase.PhaseInterceptorChain -2191 [main] DEBUG  - Chain [email protected] 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]be5
2014-09-16 09:20:02,552 - org.apache.cxf.phase.PhaseInterceptorChain -2194 [main] DEBUG  - Adding interceptor org.apache.cxf.bindi[email protected]27763e5f to phase write-ending
2014-09-16 09:20:02,559 - org.apache.cxf.phase.PhaseInterceptorChain -2201 [main] DEBUG  - Chain [email protected] 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 [email protected]
2014-09-16 09:20:02,562 - org.apache.cxf.phase.PhaseInterceptorChain -2204 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,584 - org.apache.cxf.phase.PhaseInterceptorChain -2226 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.bindi[email protected]27763e5f
2014-09-16 09:20:02,585 - org.apache.cxf.phase.PhaseInterceptorChain -2227 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,589 - org.apache.cxf.phase.PhaseInterceptorChain -2231 [main] DEBUG  - Invoking handleMessage on interceptor org.apache.cxf.interceptor.MessageSenderInterc[email protected]
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: [[email protected]]
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]b07, [email protected], [email protected], [email protected]]
2014-09-16 09:20:02,777 - org.apache.cxf.endpoint.ClientImpl -2419 [main] DEBUG  - Interceptors contributed by binding: [[email protected], [email protected], [email protected]766eae77, [email protected]f, [email protected]c7416a, [email protected]197cbe3, [email protected]ade58, [email protected]b7647, or[email protected]7bc230bf]
2014-09-16 09:20:02,777 - org.apache.cxf.endpoint.ClientImpl -2419 [main] DEBUG  - Interceptors contributed by databinging: [or[email protected]a0e8b13]
2014-09-16 09:20:02,777 - org.apache.cxf.phase.PhaseInterceptorChain -2419 [main] DEBUG  - Adding interceptor [email protected] to phase receive
2014-09-16 09:20:02,778 - org.apache.cxf.phase.PhaseInterceptorChain -2420 [main] DEBUG  - Adding interceptor [email protected]b07 to phase post-logical
2014-09-16 09:20:02,778 - org.apache.cxf.phase.PhaseInterceptorChain -2420 [main] DEBUG  - Adding interceptor [email protected] to phase pre-invoke
2014-09-16 09:20:02,778 - org.apache.cxf.phase.PhaseInterceptorChain -2420 [main] DEBUG  - Adding interceptor [email protected] to phase pre-invoke
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected] to phase read
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected] to phase receive
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected] to phase post-stream
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected]766eae77 to phase read
2014-09-16 09:20:02,779 - org.apache.cxf.phase.PhaseInterceptorChain -2421 [main] DEBUG  - Adding interceptor [email protected]f to phase unmarshal
2014-09-16 09:20:02,780 - org.apache.cxf.phase.PhaseInterceptorChain -2422 [main] DEBUG  - Adding interceptor [email protected]c7416a to phase unmarshal
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor [email protected]197cbe3 to phase read
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor [email protected]ade58 to phase read
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor [email protected]b7647 to phase post-protocol
2014-09-16 09:20:02,782 - org.apache.cxf.phase.PhaseInterceptorChain -2424 [main] DEBUG  - Adding interceptor or[email protected]7bc230bf to phase pre-protocol
2014-09-16 09:20:02,783 - org.apache.cxf.phase.PhaseInterceptorChain -2425 [main] DEBUG  - Adding interceptor or[email protected]a0e8b13 to phase post-protocol
2014-09-16 09:20:02,783 - org.apache.cxf.phase.PhaseInterceptorChain -2425 [main] DEBUG  - Chain [email protected] 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 [email protected]
2014-09-16 09:20:02,807 - org.apache.cxf.phase.PhaseInterceptorChain -2449 [main] DEBUG  - Adding interceptor [email protected] to phase pre-invoke
2014-09-16 09:20:02,808 - org.apache.cxf.phase.PhaseInterceptorChain -2450 [main] DEBUG  - Chain [email protected] 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 [email protected]
2014-09-16 09:20:02,808 - org.apache.cxf.phase.PhaseInterceptorChain -2450 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,867 - org.apache.cxf.phase.PhaseInterceptorChain -2509 [main] DEBUG  - Adding interceptor [email protected] to phase pre-invoke
2014-09-16 09:20:02,877 - org.apache.cxf.phase.PhaseInterceptorChain -2519 [main] DEBUG  - Chain [email protected] 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 [email protected]
2014-09-16 09:20:02,879 - org.apache.cxf.phase.PhaseInterceptorChain -2521 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]197cbe3
2014-09-16 09:20:02,888 - org.apache.cxf.phase.PhaseInterceptorChain -2530 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]766eae77
2014-09-16 09:20:02,892 - org.apache.cxf.phase.PhaseInterceptorChain -2534 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]ade58
2014-09-16 09:20:02,892 - org.apache.cxf.phase.PhaseInterceptorChain -2534 [main] DEBUG  - Invoking handleMessage on interceptor or[email protected]7bc230bf
2014-09-16 09:20:02,893 - org.apache.cxf.phase.PhaseInterceptorChain -2535 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]b7647
2014-09-16 09:20:02,893 - org.apache.cxf.phase.PhaseInterceptorChain -2535 [main] DEBUG  - Invoking handleMessage on interceptor or[email protected]a0e8b13
2014-09-16 09:20:02,894 - org.apache.cxf.phase.PhaseInterceptorChain -2536 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]f
2014-09-16 09:20:02,943 - org.apache.cxf.phase.PhaseInterceptorChain -2585 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]c7416a
2014-09-16 09:20:02,945 - org.apache.cxf.phase.PhaseInterceptorChain -2587 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]b07
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
2014-09-16 09:20:02,960 - org.apache.cxf.phase.PhaseInterceptorChain -2602 [main] DEBUG  - Invoking handleMessage on interceptor [email protected]
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>

发布成功

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

时间: 2024-08-02 14:53:09

CXF 实现 webservice 并且部署在web项目中 tomcat作为容器的相关文章

使用Maven自动部署Java Web项目到Tomcat问题小记

导读 首先说说自己为啥要用maven管理项目,一个直接的原因是:我在自己电脑上开发web项目,每次部署到服务器上时都要经历如下步骤: 首先在Eclipse里将项目打包成war包 将服务器上原来的项目文件夹删掉 cd /var/lib/tomcat7/webapps sudo rm XXX.war sudo rm -rf XXX 将war包传到服务器上,比如用pscp命令上传 pscp -pw "xxx" XXX.war [email protected]:/var/lib/tomcat

Eclipse部署Maven web项目到tomcat服务器时,没有将lib下的jar复制过去的解决办法

我们在做web开发是,经常都要在eclipse中搭建web服务器,并将开发中的web项目部署到web服务器进行调试,在此,我选择的是tomcat服务器.之前部署web项目到tomcat进行启动调试都很正常,今天突然出现无法启动情况,启动过程报如下错误: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener at org.apache.catalina.loader.Webap

eclipse部署maven web项目到tomcat服务器时,没有将lib、web.xml复制过去的解决办法

我这几天在写项目的时候发现自己以前的项目能够访问,隔一段时间写的这个项目却不能够访问,没有发现代码的逻辑错,但是就是访问不了jsp页面,项目一发布就是出现404错误,后来发现原来是发布到tomcat上面的项目上面没有发布index.jsp跟web.xml文件,设置连里面的lib包也没有发布上去. 下面是我在找错误的详细过程,记录下来以防自己以后忘记. 第一步:是你要找到控制台中Servers,然后双击打开它,查看里面的信息. 第二步:配置Server中发布项目的安装目录 后来我发现我发布项目到t

eclipse部署非web项目到tomcat

从svn检出的项目,假如是非web项目.如何部署到tomcat? 1,确定tomcat设置是否正确: 双击图中tomcat: 确定设置如下: 2,项目上点击右键属性,勾选如图: 3,其实以上两步之后就可以了,如果还不能部署,那么: 修改workspace,项目/settings 中的 org.eclipse.wst.common.project.facet.core.xml文件<installed facet="jst.web" version="3.0"/&

在web项目中使用cxf开发webservice,包含spring支持

本文主要介绍了,如何使用cxf内置的例子,学会开发webserivce,在web项目中使用,且包含spring支持. webserivce的开发可以使用cxf或者axis,好像还有httpclient等等.以前也多次研究过,上网搜过很多别人的例子来看,写过代码下来,但没有总结过,少废话,上干货. 1. 到cxf的官网下载jar包.我用的是以前下载下来的apache-cxf-2.7.6.zip,并非最新版本.下载完成后,解压后,目录结构如下左图: 打开其中的samples文件夹,其内包含了很多例子

在java web项目中集成webservice

公司要求在项目中加入webservice服务,因为项目中使用了spring框架,所以在这里使用与spring兼容性较好的cxf来实现 cxf所需jar包 spring的jar包就不贴了 一:创建webservice服务器 1)创建一个服务接口 package com.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorld { public S

基于IBM Bluemix部署Java Web项目实战演练

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要介绍了IBM Bluemix,并详细说明了如何部署Java Web项目,如何添加MySql服务.最后,提出了自己的一些看法.  文章目录 一.Bluemix简单介绍 二.BlueMix空间申请试用 三.BlueMix创建Cloud Foundry应用 四.添加新的服务 五.总结 一.Bluemix 简单介绍 1.Bluemix 带来了什么        Bluemix 致力于解

阿里云部署Java web项目初体验

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在阿里云上安装JDK.Tomcat以及其配置过程.最后以一个实例来演示在阿里云上部署Java web项目. 本文实例访问:http://120.24.19.24:8080/JavaHelloWorld-0.0.1-SNAPSHOT/ (到2016.3.9就不能访问了) 本文实例下载: 一.准备工作 1.注册账号 下载完成后.可到这里https://free.aliyun

linux系统上部署一个web项目

对于apache开源项目中tomcat的认识,大多停留在Windows下,这次我通过一个简单的实例来介绍一下在linux下如何搭建tomcat环境,并且部署一个web项目. 先从基本安装开始,可别小看linux下的文件安装,那可不是windows下点击next就可以完成,但也并不复杂,重要的是我们学会怎么用快速理解和掌握它,那么一切就变得容易多了,开始吧,当然在安装部署tomcat之前必须先安装好jdk1.6的环境,具体见上一遍博客linux下jdk的安装. 1.先从Apache的官方网站下载下