文档章节

Webservices in JDK 6

子木007
 子木007
发布于 2011/04/15 17:55
字数 627
阅读 299
收藏 1

Webservices in JDK 6

Posted by  vivekp on December 12, 2006 at 8:33 PM EST
Java SE 6 is out! Among other things it has exciting new end-to-end web services stack  -  JAXWS 2.0 FCS RI. Couple of main things you should know:
  • JAXWS Tools wsimport and wsgen part of JDK
  • Simplified deployment using Endpoint API and light-weight HTTP Server in JDK
  • Uses JAXB 2.0, also part of JDK6, for all data binding needs.
  • Also uses StaxSAAJ 1.3 for message processing. Both these jars are part of JDK 6.

Web service Endpoint

Lets start with a POJO annotated with @WebService annotation. This annotation tells JAXWS that its a Web Service endpoint. You may like to annotate the methods that will be exposed as web services method with @WebMethod annotation. You dont need to specify it if all the methods will be exposed as web services method.

Calculator.java
 package example;import javax.jws.WebService;import javax.jws.WebService;import javax.xml.ws.Endpoint;@WebService public class Calculator {
    @WebMethod     public int add(int a, int b) {
        return a+b;
    }
    
    public static void main(String[] args){
        // create and publish an endpoint
        Calculator calculator = new Calculator();
        Endpoint endpoint = Endpoint.publish("http://localhost:8080/calculator", calculator);        
    }
}

Now you need to do 2 things to build and publish this endpoint:

  • Run apt to compile and generate required wrapper classes
    • apt -d sample example/Calculator.java
  • Publish
    • java -cp sample example.Calculator

Thats it! You have deployed your web services endpoint. Now, try accessing the deployed WSDL by typing http://localhost:8080/calculator?wsdl in your web browser to see the published WSDL. No need to provide deployment descriptor, starting a container etc. Its all done for you internally by JAXWS using light-weight HTTP server available in JDK 6. In short - extremely fast prototyping!

Web Services Client

Lets see how we develop a client based on proxy.

Run wsimport

You would run wsimport on the deployed WSDL URL:

wsimport -p client -keep http://localhost:8080/calculator?wsdl

This step will generates and compile some classes. Notice -keep switch, you need it to keep the generated Java source files. By default wsimport only leaves behind the compiled class files. In this example the classes that matters are

  • Calculator.java - Service Endpoint Interface or SEI
  • CalculatorService - Generated Service, instantiate it to get the proxy

Invoke the endpoint

CalculatorApp.java

package client;class CalculatorApp {
	public static void main(String args[]){
        /**
         * Instantiate the generated Service
         */ 
    	CalculatorService service = new CalculatorService();
        
        /**
         * Get the port using port getter method generated in CaculatorService
         */
        Calculator calculatorProxy = service.getCalculatorPort();
        
        /**
         * Invoke the remote method
         */
        int result = calculatorProxy.add(10, 20);
        System.out.println("Sum of 10+20 = "+result);
	}
}

Now that you have your client code is ready simply compile it:

javac -cp . CalculatorApp.java

and run:

java -cp . client.CalculatorApp

It will print:

Sum of 10+20 = 30

Running latest JAXWS RI on JDK6

The obvious question might be that how would you use latest JAXWS 2.1 RI on top of JDK6. JAXWS 2.1 RI is feature complete and we are busy fixing bug. For list of JAXWS 2.1 features and plan refer to the JAXWS 2.1 roadmap.

All you need to do is to use Endorsed Directory Mechanism  to point to the lib directory in JAXWS intallation:

Runtime

  • java -cp . -Djava.endorsed.dirs=$JAXWS_HOME/lib client.CalculatorApp

Tools

  • Run the tools from JAXWS distribution
    • export JAXWS_HOME to your JAXWS distribution installation
    • $JAXWS_HOME/bin/wsimport
    • $JAXWS_HOME/bin/wsgen
  • For wsimport set WSIMPORT_OPTS="-Djava.endorsed.dirs=$JAXWS_HOME/lib"
  • For wsgen set WSGEN_OPTS="-Djava.endorsed.dirs=$JAXWS_HOME/lib"

Use JDK 6 to develop your web services application and continue providing feedback to users@jax-ws.dev.java.net and if you find an issue report them at IssueTracker.

Related Topics >>

本文转载自:http://weblogs.java.net/blog/vivekp/archive/2006/12/webservices_in.html

共有 人打赏支持
上一篇:
子木007

子木007

粉丝 75
博文 6
码字总数 947
作品 0
徐汇
私信 提问
Web Services(1)-入门介绍及实战

1.Web Services 简介 Web Services, 即 "Web 服务", 简称 "WS", 其实就是"基于Web 服务" , 所谓的服务是双方的,有服务的提供方,也有服务的需求方, 通常是服务提供方发布服务,服务需求方调用......

陈小扁
2016/03/28
168
0
C# 调用 jax-ws 时的问题

webservices 是java写的,用 JDK自带的EndPoint发布,客户端是用C#调用, 在服务器端抛出异常,尽管执行是正常的。 Unsupported Content-Type: application/soap+xml;charset=UTF-8 Support...

子木007
2011/04/28
3.5K
4
Using JDK webservice API create Web service

1、Java JDK 环境apt命令 C:Documents and SettingsAdministrator>apt -help用法: apt <apt and javac options> <source files>其中, apt 选项包括:-classpath <路径> 指定查找用户类文件和注......

boonya
2013/05/29
0
0
常用WebService一览表

天气预报Web服务,数据来源于中国气象局 Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmx Disco :http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?d......

Rella蕾拉
2012/03/15
0
0
互联网 免费的WebService接口

股票行情数据 WEB 服务(支持香港、深圳、上海基金、债券和股票;支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebServices/StockInfoWS.asmx 复制 Endpoint Disco: ht...

庞陆阳
2016/10/28
7
0

没有更多内容

加载失败,请刷新页面

加载更多

RocketMQ高性能之底层存储设计

说在前面 RocketMQ在底层存储上借鉴了Kafka,但是也有它独到的设计,本文主要关注深刻影响着RocketMQ性能的底层文件存储结构,中间会穿插一点点Kafka的东西以作为对比。 例子 Commit Log,一...

薛定谔的旺
16分钟前
2
0
实战:基于Spring Boot快速开发RESTful风格API接口

写在前面的话 这篇文章计划是在过年期间完成的,示例代码都写好了,结果亲戚来我家做客,文章没来得及写。已经很久没有更新文章了,小伙伴们,有没有想我啊。言归正传,下面开始,今天的话题...

JAVA_冯文议
23分钟前
0
0
Anaconda3 5.0.0中配置Python 3.6 + TensorFlow环境

参考:https://blog.csdn.net/hu_zhenghui/article/details/78156193

Pasenger
28分钟前
0
0
Nginx反向代理Tomcat配置

Nginx代理Tomcat 为什么要为Tomcat配置反向代理? 1)如果同一台机器既有nginx又有Tomcat,则会产生端口冲突2)我们需要把8080端口变成80端口3)nginx对于静态的请求速度上要优于Tomcat,T...

wzb88
41分钟前
10
0
JavaScript email邮箱/邮件地址的正则表达式及分析

在做用户注册时,常会用到邮箱/邮件地址的正则表达式。本文列举了几种方案,大家可以根据自己的项目情况,选择最适合的方案。 方案1 (常用) 规则定义如下: 以大写字母[A-Z]、小写字母[a-z]...

前端小攻略
43分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部