(五)web服务中的异常处理

一、服务端发布服务

package com.webservice;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface IExceptionService {

    @WebResult(name = "addResult")
    public int add(@WebParam(name = "x")
    int x, @WebParam(name = "y")
    int y) throws Exception;

    @WebResult(name = "subtractResult")
    public int subtract(@WebParam(name = "x")
    int x, @WebParam(name = "y")
    int y) throws RuntimeException;

    @WebResult(name = "divideResult")
    public int divide(@WebParam(name = "x")
    int x, @WebParam(name = "y")
    int y) throws DivideException;
}
package com.webservice;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebService;

@WebService(endpointInterface = "com.webservice.IExceptionService")
public class ExceptionImpl implements IExceptionService {

    public int add(int x, int y) throws Exception {
        if (x == 0 || y == 0) {
            throw new Exception("add中Exception====X或者Y不能为0");
        } else {
            return x + y;
        }
    }

    public int subtract(int x, int y) throws RuntimeException {
        if (x == 0 || y == 0) {
            throw new RuntimeException("subtract中RunTimeException===X或者Y不能为0");
        } else {
            return x + y;
        }
    }

    public int divide(int x, int y) throws DivideException {
        if (x == 0 || y == 0) {
            throw new DivideException("divide中DivideException===X或者Y不能为0");
        } else {
            return x + y;
        }
    }

}
  •   自定义异常
package com.webservice;

public class DivideException extends Exception {

    public DivideException() {
        super();
    }

    public DivideException(String arg0, Throwable arg1, boolean arg2,
            boolean arg3) {
        super(arg0, arg1, arg2, arg3);
    }

    public DivideException(String arg0, Throwable arg1) {
        super(arg0, arg1);
    }

    public DivideException(String arg0) {
        super(arg0);
    }

    public DivideException(Throwable arg0) {
        super(arg0);
    }

}
package com.publish;

import javax.xml.ws.Endpoint;

import com.webservice.BeanImpl;
import com.webservice.DateImpl;
import com.webservice.ExceptionImpl;
import com.webservice.ListImpl;
import com.webservice.MapImpl;
import com.webservice.StrImpl;

public class TestPublish {
    public static void main(String[] args) {
 

        Endpoint.publish("http://localhost:9090/ExceptionService",
                new ExceptionImpl());
        System.out.println("发布成功...");
    }
}

 

二、客户端

package com.exception;

public class TestMain {
    public static void main(String[] args) {
        //TestMain.add();

        // TestMain.subtract();
        
        TestMain.divide();
    }


    private static void add() {
        // 服务端没有异常
        // 客户端:com.exception.Exception_Exception
        IExceptionService exceptionService = new ExceptionImplService()
                .getExceptionImplPort();

        try {
            exceptionService.add(1, 0);
        } catch (Exception_Exception e) {
            Exception exception = e.getFaultInfo();

            String server_msg = exception.getMessage();

            System.out.println(server_msg);

            e.printStackTrace();

        }

    }

    private static void subtract() {
        // 服务端的异常:java.lang.RuntimeException
        // 客户端的异常:javax.xml.ws.soap.SOAPFaultException
        IExceptionService exceptionService = new ExceptionImplService()
                .getExceptionImplPort();

        exceptionService.subtract(3, 0);
    }
    
    private static void divide() {
        IExceptionService exceptionService = new ExceptionImplService()
        .getExceptionImplPort();
        
        try {
            exceptionService.divide(1, 0);
        } catch (DivideException_Exception e) {
            System.out.println(e.getFaultInfo().getMessage());
            e.printStackTrace();
        }
        
    }

    
}

 

posted @ 2017-10-12 14:10  shyroke、  阅读(624)  评论(0编辑  收藏  举报
作者:shyroke 博客地址:http://www.cnblogs.com/shyroke/ 转载注明来源~