The RandService
in the first example (see Example 4-1) combines, in a single source file,
what JAX-WS calls the SEI (Service Endpoint Interface) and
the SIB (Service Implementation Bean). The SEI specifies, at a high level that befits an interface,
the service operations, and the SIB provides an implementation of the operations. A SIB can be one of the following:
-
A POJO class such as
RandService
annotated as@WebService
and encapsulating service operations, each annotated as a@WebMethod
. -
A
@Stateless
Session EJB that is likewise annotated as a@WebService
. EJBs in general predate JAX-WS; hence, this second type of SIB is an inviting way to expose legacy EJBs as web services.
Chapter 7 covers the EJB implementation of a @WebService
.
For now, the SIBs will be POJO classes. For convenience, most of my examples take the
single-file approach, which combines the SEI and the SIB into one class annotated as
a @WebService
. The two-file approach is illustrated with the SEI RandService
(see Example 4-4)
in one file and the SIB RandImpl
(see Example 4-5) in another file. The RandService
is now an
interface
, whereas the RandImpl
is a class
that implements
this interface.
Example 4-4. The Service Endpoint Interface for the revised RandService
package
rand2
;
import
javax.jws.WebService
;
import
javax.jws.WebMethod
;
import
java.util.Random
;
@WebService
public
interface
RandService
{
@WebMethod
public
int
next1
();
@WebMethod
public
int
[
]
nextN
(
final
int
n
);
}
Example 4-5. The Service Implementation Bean for the revised RandService
package
rand2
;
import
javax.jws.WebService
;
import
javax.jws.WebMethod
;
import
java.util.Random
;
@WebService
(
endpointInterface
=
"rand2.RandService"
)
public
class
RandImpl
implements
RandService
{
private
static
final
int
maxRands
=
16
;
@WebMethod
public
int
next1
()
{
return
new
Random
().
nextInt
();
}
@WebMethod
public
int
[
]
nextN
(
final
int
n
)
{
final
int
k
=
(
n
>
maxRands
)
?
maxRands
:
Math
.
abs
(
n
);
int
[
]
rands
=
new
int
[
k
];
Random
r
=
new
Random
();
for
(
int
i
=
0
;
i
<
k
;
i
++)
rands
[
i
]
=
r
.
nextInt
();
return
rands
;
}
}
In the SIB class RandImpl
, the @WebService
interface has an attribute, the key/value pair:
endpointInterface
=
"rand2.RandService"
that names the SEI. It is still important for the
class RandImpl
to employ the standard implements
clause because only the implements
clause prompts the compiler to make sure that the public methods declared in the SEI, in this
case the two methods annotated with @WebMethod
, are defined appropriately in the SIB.
The revised RandService
has the same functionality as the original. The Endpoint
publisher
changes slightly:
Endpoint
.
publish
(
url
,
new
RandImpl
());
// SIB, not SEI
The second argument to the publish
changes to RandImpl
precisely because, in the revision,
RandService
is an interface. In general, the second argument to the static
version of
publish
is always the SIB. In a single-file case such as the original version of RandService
, a
single class is the combined SEI and SIB.
Get Java Web Services: Up and Running, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.