By GokiSoft.com|
11:42 27/02/2021|
Java Web + WebService
[Share Code] Tạo dự án mẫu Web Service (Calculator Web Service) - Lập trình Web Service
1. Web Service
#CalculatorWS.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech.ws;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
* CalculatorWS -> WSDL (SOAP API) -> Gom cac API chuc nang.
* @author DiepTV
*/
@WebService(serviceName = "CalculatorWS")
public class CalculatorWS {
/**
* This is a sample web service operation
* hello method -> tuong la 1 API de su dung sau nay.
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
@WebMethod
public float cong(@WebParam(name = "x") float x, @WebParam(name = "y") float y) {
return x + y;
}
@WebMethod(operationName = "api_calculator_tru")
public float tru(@WebParam(name = "param1") float x, @WebParam(name = "param2") float y) {
return x - y;
}
@WebMethod
public float nhan(@WebParam(name = "x") float x, @WebParam(name = "y") float y) {
return x * y;
}
@WebMethod
public float chia(@WebParam(name = "x") float x, @WebParam(name = "y") float y) throws Exception {
if(y == 0) {
throw new Exception("Yeu cau y khac 0");
}
return x/y;
}
}
#TestWebService.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech.ws;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
*
* @author DiepTV
*/
@WebService(serviceName = "TestWebService")
public class TestWebService {
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
}
2. Client
#CalculatorAPI.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech;
/**
*
* @author DiepTV
*/
public class CalculatorAPI {
public static float cong(float x, float y) {
aptech.CalculatorWS_Service service = new aptech.CalculatorWS_Service();
aptech.CalculatorWS port = service.getCalculatorWSPort();
return port.cong(x, y);
}
public static float tru(float x, float y) {
aptech.CalculatorWS_Service service = new aptech.CalculatorWS_Service();
aptech.CalculatorWS port = service.getCalculatorWSPort();
return port.apiCalculatorTru(x, y);
}
public static float nhan(float x, float y) {
aptech.CalculatorWS_Service service = new aptech.CalculatorWS_Service();
aptech.CalculatorWS port = service.getCalculatorWSPort();
return port.nhan(x, y);
}
public static float chia(float x, float y) throws Exception_Exception {
aptech.CalculatorWS_Service service = new aptech.CalculatorWS_Service();
aptech.CalculatorWS port = service.getCalculatorWSPort();
return port.chia(x, y);
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aptech;
import java.util.Scanner;
/**
*
* @author DiepTV
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x: ");
float x = scan.nextFloat();
System.out.println("Nhap y: ");
float y = scan.nextFloat();
float result = CalculatorAPI.cong(x, y);
System.out.println("Result: " + result);
}
}
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)