How to invoke web services on WSDL URL in Java?

前端 未结 1 2034
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 04:40

I need to invoke some web service methods within a java web application that I\'m building.

E.g each time a user signs up, I want to call the newUser me

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 05:14

    Run wsimport on the deployed WSDL URL , you can run it from your JDK:

    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.

    Calculator.java - Service Endpoint Interface or SEI
    CalculatorService - Generated Service, instantiate it

    public class MyClientServiceImpl {
        public static void main(String args[]){
    
        @Override
        public Integer add(int a , int b) {
           CalculatorService service = new CalculatorService();
           Calculator calculatorProxy = service.getCalculatorPort();            
            /**
             * Invoke the remote method
             */
            int result = calculatorProxy.add(10, 20);
            System.out.println("Sum of 10+20 = "+result);
        }
    }
    

    If you are using the Java EE 6 supported container then you can use it in this way ,

    public class MyClientServiceImpl implements MyClientService {
    
        @WebServiceRef(wsdlLocation = "http://localhost:8080/calculator?wsdl", 
    value = CalculatorService.class)
        private Calculator service;
    
        @Override
        public Integer add(int a , int b) {
            return service.add(a,b);
        }
    }
    

    0 讨论(0)
提交回复
热议问题