웹 서비스 제1강

2798 단어 webservice
1. 서버 구축
1.1 인터페이스 만들기
package org.first.service;

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

@WebService
public interface IMyService {
	
	@WebResult(name="addResult")
	public int add(@WebParam(name="a")int a,@WebParam(name="b")int b);
	
	@WebResult(name="minusResult")
	public int minus(@WebParam(name="a")int a,@WebParam(name="b")int b);
	
	@WebResult(name="loginUser")
	public User login(@WebParam(name="username")String username,@WebParam(name="password")String password);

}

1.2 인터페이스 구현 클래스 만들기
package org.first.service;

import javax.jws.WebService;

@WebService(endpointInterface="org.first.service.IMyService")
public class MyServiceImpl implements IMyService {

	@Override
	public int add(int a, int b) {
		System.out.println(a+"+"+b+"="+(a+b));
		return a+b;
	}

	@Override
	public int minus(int a, int b) {
		System.out.println(a+"-"+b+"="+(a-b));
		return a-b;
	}

	@Override
	public User login(String username, String password) {
		System.out.println(username+" is logining");
		User user = new User();
		user.setId(1);
		user.setUsername(username);
		user.setPassword(password);
		return user;
	}

}

1.3 서비스 개시
package org.first.service;

import javax.xml.ws.Endpoint;

public class MyServer {

	public static void main(String[] args) {
		String address = "http://localhost:8888/ns";
		Endpoint.publish(address, new MyServiceImpl());
	}

}

2 클라이언트 구축
package org.first.service;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class TestClient {
	public static void main(String[] args) {
		try {
			// wsdl url
			URL url = new URL("http://localhost:8888/ns?wsdl");
			// Qname 
			QName sname = new QName("http://service.first.org/", "MyServiceImplService");
			// 
			Service service = Service.create(url,sname);
			// 
			IMyService ms = service.getPort(IMyService.class);
			System.out.println(ms.login("wxh", "wr"));
			// , IMyServie 
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기