Developing Web Services using jax-ws, spring and maven

tastelessbeachInternet and Web Development

Nov 12, 2013 (3 years and 11 months ago)

81 views

Developing Web Services using jax
-
ws,
spring and

m
aven




(1)

Define service interface


package

uk.co.mo.training.ws;


import

javax.jws.WebMethod;

import

javax.jws.WebParam;

import

javax.jws.
WebService
;

@SuppressWarnings
(
"restriction"
)

@WebService

public

interface

TemperatureService {




@
WebMethod


public

double

toCelsius(
@WebParam
(name=
"fahrenheit"
)
double

fahrenheit);


@
WebMethod


public

double

toFahrenheit(
@WebParam
(name=
"celsius"
)
double

celsius);


}


(2)

Define service Implementation


package

uk.co.mo.training.ws;


import

javax.jws.WebService;


import

org.springframework.stereotype.Service;

@SuppressWarnings
(
"restriction"
)

@WebService
(endpointInterface =
"uk.co.mo.training.ws.TemperatureService"
,serviceName=
"temperatureService"
)

@Service

publi
c

class

TemperatureServiceImple
implements

TemperatureService {




public

double

toCelsius(
double

fahrenheit) {



double

celsius = (5.0 / 9.0) * (fahrenheit
-

32.0);



//C° = (5 / 9)x (F°
-

32)



return

celsius;


}




public

double

toFahrenheit(
double

celsius) {



double

fahrenheit

= (celsius *1.8)+32.0;



//F° = (C° x 1.8) + 32



return

fahrenheit
;


}


}


(3)

Web.xml


<!
DOCTYPE

web
-
app

PUBLIC


"
-
//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"


"http://java.sun.com/dtd/web
-
app_2_3.dtd"

>


<
web
-
app
>




<!
--

spring framework context configuration
--
>


<
context
-
param
>


<
param
-
name
>
contextConfigLocation
</
param
-
name
>


<
param
-
value
>
/WEB
-
INF/
conf
/main
-
application
-
context.xml
</
param
-
value
>


</
context
-
param
>






<!
--

Spring Context Listener
--
>


<
listener
>


<
listener
-
class
>
org.springframework.web.context.ContextLoaderListener
</
listener
-
class
>



</
listener
>




<!
--

this is for JAX
-
WS

commons spring ext.

--
>


<
servlet
>


<
servlet
-
name
>
jaxws
-
servlet
</
servlet
-
name
>


<
servlet
-
class
>


com.sun.xml.ws.transport.http.servlet.WSSpringServlet


</
servlet
-
class
>


<
load
-
on
-
startup
>
1
</
load
-
on
-
startup
>


</
servlet
>




<
servlet
-
mapping
>


<
servlet
-
name
>
jaxws
-
servlet
</
servlet
-
name
>


<
url
-
pattern
>
/
webse
rvice
/*
</
url
-
pattern
>


</
servlet
-
mapping
>



</
web
-
app
>



(4)

Spring beans configuration

using annotations
main
-
application
-
context.xml


<
beans

xmlns
=
"http://www.springframework.org/schema/beans"


xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema
-
instance"


xmlns:aop
=
"http://www.springframework.org/schema/aop"


xmlns:tx
=
"http://www.springframework.org/schema/tx"


xmlns:context
=
"http://www.springframework.org/schema/context"


xsi:schemaLocation
=
"


http://www.springframework.
org/schema/beans
http://www.springframework.org/schema/beans/spring
-
beans
-
2.5.xsd


http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring
-
tx
-
2.5.xsd


http://www.springframework.org/schema/aop
http://w
ww.springframework.org/schema/aop/spring
-
aop
-
2.5.xsd


http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring
-
context
-
2.5.xsd"
>



<!

scan annotations
--
>



<
context:component
-
scan

base
-
package
=
"uk.co.mo.training.ws"
/>


<
context:annotation
-
config
/>


<!
--

Web Services
--
>



<
import

resource
=
"ws
-
context.xml"
/>





</
beans
>



(5)

Spring
configurations

for Web Services
:
ws
-
context.xml


<?
xml

version
=
"1.0"

encoding
=
"UTF
-
8"
?>

<
beans

xmlns
=
"http://www.springframework.org/schema/beans"


xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema
-
instance"


xmlns:ws
=
"http://jax
-
ws.dev.java.net/spring/core"


xmlns:wss
=
"http://jax
-
ws.dev.java.net/spring/servlet"


xsi:schemaLocation
=
"


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring
-
beans
-
2.0.xsd


http://jax
-
ws.dev.java.net/spring/core


http://jax
-
ws.dev.java.net/spring/core.xsd


http://jax
-
ws.dev.java.n
et/spring/servlet


http://jax
-
ws.dev.java.net/spring/servlet.xsd"
>




<!
--

Temperature Web Service
--
>


<
wss:binding

url
=
"/webservice/temperaturewebservice"
>



<
wss:service
>



<
ws:service

bean
=
"#temperatureServiceImple"
>




</
ws:service
>



</
wss:service
>



</
wss:binding
>




</
beans
>


(6)

Add
maven
plugins in the pom.xml :
jaxws
-
maven
-
plugin


.....


<
build
>


<
finalName
>
mo
-
ws
</
finalName
>


<
plugins
>


<
plugin
>


<
groupId
>
org.apache.maven.plugins
</
groupId
>


<
artifactId
>
maven
-
compiler
-
plugin
</
artifactId
>


<
version
>
2.3.2
</
version
>


<
configuration
>


<
source
>
1.5
</
source
>


<
target
>
1.5
</
target
>


</
configuration
>


</
plugin
>





<
plugin
>





<
groupId
>
org.codehaus.mojo
</
groupId
>





<
artifactId
>
jaxws
-
maven
-
plugin
</
artifactId
>





<
version
>
1.10
</
version
>








<
executions
>










<
execution
>






<
id
>
TemperatureWS
</
id
>






<
configuration
>







<
sei
>
uk.co.mo.training.ws.TemperatureServiceImple
</
sei
>







<
genWsdl
>
true
</
genWsdl
>







<
keep
>
true
</
keep
>







<
verbose
>
true
</
verbose
>






</
configuration
>






<
phase
>
package
</
phase
>






<
goals
>







<
goal
>
wsgen
</
goal
>






</
goals
>





</
execution
>




</
executions
>






</
plugin
>


</
plugins
>


</
build
>

....


(7)

Using maven build deployment: (1)
mvn clean package

and again

(2)

mvn package


(8)

Deploy in the tomcat


...

<
Context




docBase
=
"D:/projects/mo
-
ws
-
project/target/mo
-
ws"




path
=
"/ws"
>








</
Context
>

...


(9)

http://localhost:8080/ws/webservice/temperaturewebservice

(10)

Using
soapUI

test web service



Resources



Jax
-
ws commons

spring

http://jax
-
ws
-
commons.java.net/spring/



wsgen

tool

http://download.oracle.com/javase/6/docs/technotes/tools/share/wsgen.html



sopaUI

-

http://www.soapui.org/Getting
-
Started/installing
-
on
-
windows.html