Szoftverfejlesztés J2EE platformon - Labor: Web Service
A VIK Wikiből
Web Service laborok más tárgyakból
- Integrált információs rendszerek — Visual Studio 2005 + Web Service Enhancement 3
- Elosztott rendszerek — Visual Studio 2003
Devizaváltó Web Service implementálása
- New Project / Enterprise Application
- Name: WSLabor EJB es Web Application Module is kell
- New Project / Web Application
- Name: WSLaborService
- Add to Enterprise Application: WSLabor
- WSLaborService / Source Packages / New Java Class...
- Name: RateDescriptor
- Package: ws.labor
- WSLaborService / Source Packages / New Java Class...
- Name: Converter
- Package: ws.labor
RateDescriptor bean
package ws.labor; public class RateDescriptor { private String from; private String to; public RateDescriptor() { } public RateDescriptor(String from, String to) { this.setFrom(from); this.setTo(to); } // getterek es setterek generalasa: // context menuben Refactor / Encapsulate Fields... public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } // Ctrl+I: felsorolja az override-olhato metodusokat public boolean equals(Object obj) { if (!(obj instanceof RateDescriptor)) return false; if (this==obj) return true; RateDescriptor d = (RateDescriptor) obj; return from.equals(d.from) && to.equals(d.to); } public String toString() { return from + " -> " + to; } public int hashCode() { return from.hashCode() ^ to.hashCode(); } }
Converter service
package ws.labor; import java.util.HashMap; import java.util.Map; import javax.jws.WebMethod; import javax.jws.WebService; @WebService() public class Converter { Map<RateDescriptor, Double> rates = new HashMap<RateDescriptor, Double>(); public Converter() { rates.put(new RateDescriptor("HUF", "EUR"), 260.0); rates.put(new RateDescriptor("HUF", "USD"), 210.0); } @WebMethod public Double getRate1(RateDescriptor d) { return rates.get(d); } // Web Service nem tamogatja az overload-olt metodus neveket @WebMethod public Double getRate2(String from, String to) { return rates.get(new RateDescriptor(from, to)); } }
Deploy, majd a szerver logból másoljuk ki az url-t es írjuk utána, hogy =?wsdl=. =<xsd:schema>/<import>@schemaLocation= alatt vannak a metódus deklarációk.
Devizaváltó Web Service meghívása
- WSLabor-war / Source Packages / New Servlet
- name: Converter
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet Converter</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet Converter at " + request.getContextPath () + "</h1>"); out.println("<form method='post'>"); out.println("From: <input type='text' name='from'><br>"); out.println("To: <input type='text' name='to'><br>"); out.println("<input type='submit' value='Get Rate!'><br>"); out.println("</form>"); String from = request.getParameter("from"); String to = request.getParameter("to"); if (from!=null && to!=null) { } out.println("</body>"); out.println("</html>"); out.close(); }
- WSLabor-war / New / Web Service Client
- Project: Browse-zal valasszuk ki a Convertert
- Package: ws.labor.client
- WSLabor-war / Web Service References / ConverterService / ConverterService / ConverterPort / getRate2-t huzzuk at az =if (from!=null && to!=null) { }= blokk belsejebe.
- Deploy
- Ha nem megy, mert nem talalja a web service-t, Runtime / Servers / Sun AppServer / Applications / Web Applications / WSLaborService / Undeploy, majd deploy ujra.
Járatkereső Web Service implementálása EJB modulban
- WSLabor-ejb-ben New Java Class
- Name: AirLineBean
- Package: ws.labor.ejb
public class AirLineBean { private int id; private String source, destination; private double price; private String currency; // + default es parameterezett konstruktorok // + getterek es setterek }
- Hozzunk letre egy Stateless Session beant ugyanabba a package-be TravelSession neven.
TravelSessionBean
package ws.labor.ejb; import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import javax.jws.WebMethod; import javax.jws.WebService; @Stateless @WebService() public class TravelSessionBean implements ws.labor.ejb.TravelSessionLocal { List<AirLineBean> airlines = new ArrayList<AirLineBean>(); public TravelSessionBean() { airlines.add(new AirLineBean(1, "Budapest", "New York", 200000.0, "HUF")); airlines.add(new AirLineBean(2, "Budapest", "New York", 180000.0, "HUF")); airlines.add(new AirLineBean(3, "Budapest", "Parizs", 30000.0, "HUF")); } @WebMethod() public AirLineBean[] searchAirLine(String source, String destination) { List<AirLineBean> result = new ArrayList<AirLineBean>(); for (AirLineBean alb : airlines) { if ((source==null || alb.getSource().equals(source)) && (destination==null || alb.getDestination().equals(destination))) { result.add(alb); } } return result.toArray(new AirLineBean[result.size()]); } }
Tesztelés
- Runtime / Servers / Sun AppServer / View Admin Console
- Web Services / TravelSessionBean / Test
- Probaljuk ki ugy is, hogy a kiindulasi es a cel allomast is megadjuk es ugy is, hogy csak az egyiket.
Önálló feladat
- Keszitsunk egy webes klienst a TravelSessionBean-hez
- A kliens kerje le a talalatokat, es hivja meg a Converter service-t, ha nem stimmel a penznem.
-- Peti - 2006.11.08.