加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservices in JDK 6

发布时间:2020-12-17 01:19:30 所属栏目:安全 来源:网络整理
导读:Vivek Pandey JAXWS 2.1 EA3 Posted?|? Main?|? Running JAXWS 2.1 wsimport on JDK 1.6 ? 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

Vivek Pandey

?

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?Stax,?SAAJ 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 >>
  • Java Web Services and XML

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读