webservice中调用本地session,request

来源:互联网 发布:java招聘深圳 编辑:程序博客网 时间:2024/04/20 02:39

1利用apache cxf编写webservice(在此总结并分享,有错误不吝指教(暂时是个小菜鸟。。))

2cxf-servlet中添加元素

<jaxws:endpoint
   id="helloWorld"
   implementor="com.xxx.xxx.impl.HelloWorldServiceImp"
   address="/HelloWorld" />

3导入jetty,jetty-util.jar

4service inteface:

 @WebService
 public interface HelloWorldService{
    public String test();
}

5 service implement:

@WebService(endpointInterface = "com.xxx.xxx.HelloWorldService")
public class HelloWorldServiceImp implements HelloWorldService {
//引入webserviceContext ,提供各web元素

@Resource
 private WebServiceContext context;
 @Override
 public String test() {
  System.out.println(context.getMessageContext().get(MessageContext.SERVLET_CONTEXT));
  return "AA";
 }

}
6测试:

public class Client{


 public Client() {
  // TODO 自动生成构造函数存根
 }

 

 public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  factory.setServiceClass(HelloWorldService.class);
  factory.setAddress("http://localhost:8080/xxx/xxx/HelloWorld");
  HelloWorldService client = (HelloWorldService) factory.create();

  System.out.println(client.test());
 }

 

}

原创粉丝点击