Ejb_ Web Services for developing EJB container models

Source: Internet
Author: User
Tags webservice annotation jboss

Web services for developing EJB container models

Web Services

Web services are also a distributed technology, and the biggest difference with EJBS is that Web services are industry-standard and can span platforms and languages. While EJB is the specification of Java platform, although it can be cross-platform theoretically, it is more complicated to implement, so its application scope is limited to the Java platform. They are not the same, the Web service is biased about what the system provides, and the EJB emphasizes how to use a component to assemble these functions. Like a hard disk, it provides storage services, which is the focus of Web services, for how to assemble this hard disk, how to construct these small parts, Web services do not care, but these are the concerns of EJB.

Java EE provides two different programming models for Web services: The EJB container model and the Web container model, which describes the development of a Web service using the jax-ws2.x specification (Java API for xml-based).

Steps:

1. Create a new Web service project, create a new javaproject:webservice, and import the Ejbjar file.

2. Create a stateless session bean and publish it to WebService.

Create a new interface OrderService under Cn.hqu.service, define three methods:

Public interface OrderService {

Public abstract Stringgetusename (String name);

Public abstract Ordergetorder (String orderId);

Public abstract list<Order>getorders ();

}

New JavaBean: Class order under Cn.hqu.bean, there is a field OrderID, name. and getter, setter.

The new class Orderservicebean implements the interface OrderService in Cn.hqu.service.impl:

Package Cn.hqu.service.impl;import Java.util.arraylist;import Java.util.list;import javax.ejb.remote;import Javax.ejb.stateless;import Javax.jws.webservice;import Cn.hqu.bean.order;import Cn.hqu.service.OrderService; @Stateless @remote (orderservice.class) public class Orderservicebean implements OrderService {@Overridepublic String Getusename (String name) {return name;} @Overridepublic Order GetOrder (String orderId) {Order order = New Order (); Order.setorderid (orderId); Order.setname ("Su Zhida "); return order;} @Overridepublic list<order> getorders () {list<order> persons = new arraylist<order> (); Order order1 = New Order (); Order1.setorderid ("1"); Order1.setname ("Small su 1"); Order Order2 = New Order (); Order1.setorderid ("2"); O Rder1.setname ("Xiao Su 2");p Ersons.add (order1);p ersons.add (order2); return persons;}}

Add @webservice to the class and turn the method into the WebService method.

It is also possible to annotate methods that need to be exposed, add @webmethod to the method, and do not label the WebService method.

Package the release, use ant, copy an ant, modify the configuration name to the project name.

Perform the deploy and publish the WebService to JBoss. Check that the console was published successfully.

Go to JBoss admin to view our WebService service

http://localhost:8080/jbossws/Click View a list ofdeployed services.

You can click Http://127.0.0.1:8080/WebService/OrderServiceBean?wsdl to view WebService's description language, which can be modified by WebService annotation @webservice properties.

@WebService (targetnamespace="http://ws.hqu.cn", name="OrderService",

Servicename="Orderservicebean")

Release Deploy.

Web service development is complete and the client for the Web service is developed next.

The development steps are as follows:

1. Copy the WS-Jax jar file into Lib under the application's classpath (download path: https://jax-ws.dev.java.net).

2. Generate auxiliary classes with the Web Service client Generation tool.

3. Step three: Call WebService with the helper class.

Create a client app, new javaproject:wsclient, import Ejbjar. Create a new Lib folder.

We use the Ant task Class of the Wsimport tool to generate auxiliary classes

New Ant profile: Create a new build.xml under the project root directory

<?xml version= "1.0" encoding= "UTF-8"?>

<project name= "WSClient" default= "Wsclientgen" basedir= "." >

<property name= "Src.dir" value= "${BASEDIR}/SRC" />

<path id= "Build.classpath" description= " Set class Path " >

<fileset dir= "${basedir}/lib" >

<include name= "*.jar" />

</fileset>

</path>

<target name= "Wsclientgen" description= " Build WebService client-side helper code, refresh the project after execution " >

<taskdef name="wsimport " classname="Com.sun.tools.ws.ant.WsImport " classpathref="Build.classpath"/>

<wsimport wsdl= "HTTP://127.0.0.1:8080/WEBSERVICE/ORDERSERVICEBEAN?WSDL"

sourcedestdir= "${src.dir}" package= "Cn.hqu.ws.client" keep= "true"

verbose= "true" destdir= "${basedir}/bin" />

</target>

</project>

Executing the helper code for the ant build client call

Refresh the client project to see the generated code.

Using this code to invoke the Web service,

Create a new unit Test call Web service:

Public class wstest {

@Test

Public void GetUserName () {

Orderservicebeanservice = newOrderservicebean ();

Orderserviceorderservice = Service.getorderserviceport ();

System. out. println (Orderservice.getusename (" xiaoming "));

}

}

Console output Xiaoming proves that the method is successful.

Error: Javax.xml.ws.WebServiceException:No Content-type in the header!

Causes and solutions:

Copy the file under the Jboss_home/client directory in the JBoss4.2 to the jboss_home/lib/endorsed directory in JBoss5.1, and restart the service.

Jboss-jaxrpc.jar

Jboss-jaxws.jar

Jboss-jaxws-ext.jar

Jboss-saaj.jar

Package Juint.test;import Java.util.list;import Org.junit.test;import cn.hqu.ws.client.order;import Cn.hqu.ws.client.orderservice;import Cn.hqu.ws.client.orderservicebean;public class WSTEst {@Testpublic void GetUserName () {Orderservicebean service = new Orderservicebean (); OrderService OrderService = Service.getorderserviceport (); System.out.println (Orderservice.getusename ("xiaoming")); @Testpublic void GetOrder () {Orderservicebean service = new Orderservicebean (); OrderService OrderService = Service.getorderserviceport (); Order order =orderservice.getorder ("111"); System.out.println (Order.getorderid () + "," +order.getname ());} @Testpublic void GetOrders () {Orderservicebean service = new Orderservicebean (); OrderService OrderService = Service.getorderserviceport (); List<order> orders =orderservice.getorders (); for (Order order:orders) {System.out.println (Order.getorderid () + "," +order.getname ());}}}


When you develop a Web service, when you return to the list, you should define it in generic form, within the EJB container to interpret it, and translate it to generate the type S of the corresponding XML.

(HTTP://127.0.0.1:8080/WEBSERVICE/ORDERSERVICEBEAN?WSDL).

Code: Http://pan.baidu.com/s/1i39S4sP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.