Түрүүчийн бичлэгт webservice - ээ үүсгэсэн. Интерфейс нь
package xfire.secure.demo;
public interface ILookPeople {
public String example(String message);
}
байх бөгөөд implementation class нь дараах байдалтай байна
package xfire.secure.demo;
public class LookPeopleImpl implements ILookPeople {
public String example(String message) {
return message;
}
}
Webservice -ээ тестлэхын тулд webservice project оо server-тээ upload хийгээд
http://localhost:8080/xfiredemo/services/ гэсэн замаар хандахад
Available Services:
* LookPeople [wsdl]
Generated by XFire ( http://xfire.codehaus.org )
гэсэн бичлэг гарч байнх ёстой. Хэрвээ энэ гарч ирэхгүй бол та server-нхээ log-г шалгана уу.
Ингээд Testclient class үүсгээд test client ээ command line -с болон Myeclipse-cээ Run as Java Application гэж ажиллуулна.
public class TestClient {
public static void main(String[] args) {
Service srvcModel = new
ObjectServiceFactory().create(ILookPeople.class);
XFireProxyFactory factory =
new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloWorldURL =
"http://localhost:8080/xfiredemo/services/LookPeople";
try {
ILookPeople srvc = (ILookPeople)factory.create(srvcModel, helloWorldURL);
String result = srvc.example("Hello world");
System.out.print(result);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Үр дүн: Hello world
Та ажилттай web service бүтээлээ.
1. Collection дамжуулдаг
2. Map дамжуулдаг
3. Secure token шалгадаг болгьё.
Ингээд интэрфейсээ болон Мөн implementation class аа дараах байдалтай өөрчлье.
package xfire.secure.demo;
import java.util.Collection;
import java.util.Map;
//Generated by MyEclipse
public interface ILookPeople {
public String example(String message);
public Collection getAllAddress();
public Map getUserInformation();
}
package xfire.secure.demo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import xfire.secure.data.Address;
import xfire.secure.data.Person;
//Generated by MyEclipse
public class LookPeopleImpl implements ILookPeople {
private Collection addresses = null;
private Map userInfo = null;
private void init_address() {
// TODO Auto-generated method stub
addresses = new ArrayList<Address>();
for(int i=0; i< 10; i++){
addresses.add(new Address("MN " + i,"UB " + i, "AMAR " +i, "ROCK " + i, i ));
}
}
private void init_info(){
userInfo = new HashMap();
Collection users = new ArrayList();
for(int i=0; i< 10; i++){
//users.add(new Person("A " +i, "B" +i, "C" + i, i, new Date()));
userInfo.put(new Person("A " +i, "B" +i, "C" + i, i, new Date()), new Address("AU " + i,"CI " + i, "JOHN " +i, "BUILDING " + i, i ));
}
}
public String example(String message) {
return message;
}
public Collection getAllAddress() {
// TODO Auto-generated method stub
init_address();
return addresses;
}
public Map getUserInformation() {
// TODO Auto-generated method stub
init_info();
return userInfo;
}
}
Хэрвээ java 1.4 дээр ажилж байгаа бол энэхүү interface ээ заавал map хийсэн байх ёстой.
ILookPeople interface binding -г дараах байдалтай хийлээ.
ILookPeople.aegis.xml файлын нэр нь интерфейсын нэртэй яг адилхан мөн интерфейс байрлаж байгаа directory-т байх ёстой.
< mappings >
< mapping >
< method name="getAllAddress" >
< return-type componentType="xfire.secure.data.Address" />
</method>
< method name="getUserInformation" >
< return-type keyType="xfire.secure.data.Person" componentType="xfire.secure.data.Address" />
< /method >
< /mapping >
< /mappings >
Хэрвээ та java 1.5 -c дээш хувилбар ашиглаж байгаа бол ингэх шаардлагагүй.
Интерфейс болон implementation class дээрээ
public Collection < Address > getAllAddress();
public Map< Address, Person > getUserInformation();
гэж төрөлийг нь заагаад өгчхөд л ажиллана.
Одоо webservice ээ tomcat-даа deploy хийж өгөөд
http://localhost:8080/xfiredemo/services/LookPeople?wsdl гэсэн url -р хандахад
Websevice -н WSDL файл гарч ирнэ. WSDL = Web Service Definition Language
Хэрвээ таны webservice deploy хийгдэхгүй байвал та ямар нэг алдаа хийсэн байна. Үүнд google найз чинь туслана. Эсвэл та энд сэтгэгдлээ үлдээнэ үү.
Client -с тест хийхдээ
public class TestClient {
public static void main(String[] args) {
Service srvcModel = new
ObjectServiceFactory().create(ILookPeople.class);
XFireProxyFactory factory =
new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloWorldURL =
"http://localhost:8080/xfiredemo/services/LookPeople";
try {
ILookPeople srvc = (ILookPeople)factory.create(srvcModel, helloWorldURL);
String result = srvc.example("hello world");
System.out.print(result);
//Collection
System.out.println(">>>>>>>>>>>>>>>>>> Collection test information <<<<<<<<<<<<<<<<<<<<<<<<");
Collection collection = null;
try {
collection = srvc.getAllAddress();
} catch (Exception e) {
// TODO: handle exception
collection = null;
}
if(collection != null){
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
Address elem = (Address) iterator.next();
elem.print();
System.out.println();
}
}
System.out.println(">>>>>>>>>>>>>>>>>> Map test information <<<<<<<<<<<<<<<<<<<<<<<<");
//Map
Map map = null;
try {
map = srvc.getUserInformation();
} catch (Exception e) {
// TODO: handle exception
map = null;
}
if(map != null){
Set entries = map.entrySet();
Iterator iterator = entries != null ? entries.iterator() : null;
if(iterator != null){
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
((Person) entry.getKey()).print();
((Address) entry.getValue()).print();
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Үргэлжлэл бий ... ;)
6 сэтгэгдэл:
Sain oilgoxgui yum :(
сайн битгий хэл түй ч ойлгохгүй байна өөө кэкэкэ
amar mini ee eniig yuund ashigldag talaar ehleed bichuul zugeer bhaa yaaval eniig hergeluul zugeer bdag ch gedii mu? mannngar 2 hol komputer-iin hoorond ugugdul damjuulna ch gedii mu neg tiimerhu :D
hehe t1 ee webservice1-g ni umsh tend yamar uchirtai ni bga
bi yag ene webservice-n talaar bie daaj sudlii gej bodood net-s mgl-r medeelel haisan chin ene bichlegiig olj unshlaa. Bayarlalaa... ushuu zunduu olon hicheel taviarai. Bie daaj surch bgaa nadad zuvluurei. Bas WSDL-n talaar jaahan tailbarlaad uguurei.
vvniig ymar progrom deer bicheh ve web server gesen progrom bdag yam uu ain web control gej yu gej baigaag tailbarlah bolomjtoi yu vvniig ali es adilhan utgatai yam uu