Use SPRING+CXF to develop WebService, use annotation methods __web

Source: Internet
Author: User
Tags soap webservice annotation wsdl
The Apache CXF provides a convenient spring consolidation approach that exposes Web services and consumer Web services through annotations, Spring-labeled configurations
Various types of annotation. @WebService and @WebMethod are WSDL mapping annatotion. These Annotation relate the WSDL document element that describes the Web Service to the Java source code. @SOAPBinding is a bound annotation used to describe network protocols and formats.

1, @WebService annotation elements name,servicename and targetnamespace members are used to describe
Wsdl:porttype, Wsdl:service, and targetnamespace generate WSDL files in WebService.
2, @SOAPBinding is a binding Annotation used to describe the protocol for SOAP format and RPC.
3. The OperationName member of the @WebMethod Annotation describes the wsdl:operation, and its operation depicts
Describes the SOAPAction head in a WSDL document. This is the number of clients that must be put into the Sqapheader
Value, a constraint in SOAP 1.1.
4. The PartName member of the @WebParam Annotation describes the Wsdl:part in the WSDL document.
5. The PartName member of the @WebResult Annotation describes the value Wsdl:part uses to return the WSDL document.

For example, the following uses annotation to define a webservice:
Import java.util.List;
Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;
Import Com.cxf.pojo.User;

@WebService (targetnamespace = "http://jdk.study.hermit.org/client")
Public interface UserService {
@WebMethod (operationname= "Insert")
The public void Insert (@WebParam (name = "UserId") String userId,
@WebParam (name = "UserName") String UserName,
@WebParam (name = "UserEmail") String UserEmail,
@WebParam (name = "Userage") int userage);
@WebMethod (operationname= "Getuserbyid")
@WebResult (name = "Result")
Public User Getuserbyid (@WebParam (name= "userid") String userid);
@WebMethod (operationname= "GetAllUsers")
@WebResult (name = "Result")
Public List getallusers ();
}



The implementation classes are as follows:
Import java.util.List;
Import Javax.jws.WebService;

Import Com.cxf.dao.UserDao;
Import Com.cxf.pojo.User;
Import Com.cxf.service.UserService;

@WebService (endpointinterface= "Com.cxf.service.UserService")
public class Userserviceimpl implements UserService {

Private Userdao Userdao;
Public List getallusers () {
Return Userdao. Findalluser ();
}

Public User Getuserbyid (String userid) {
Return Userdao. Finduserbyid (userid);
}

public void Insert (string userid, String username, string useremail, int userage) {
User user= new User ();
User.setuserage (Userage);
User.setuseremail (UserEmail);
User.setuserid (userid);
User.setusername (username);
Userdao. Insert (user);
System. Out. println ("Insert successfully!");
}

public void Setuserdao (Userdao Userdao) {
this. Userdao = Userdao;
}
}
Note: The @webservice in the implementation class, where the Endpointinterface member specifies the interface implemented by the class

In spring's configuration file, you need to configure it:
The CXF XML scheam configuration file is first introduced in Applicationcontext.xml (Spring's configuration file), as follows:
< beans xmlns = "Http://www.springframework.org/schema/beans"
Xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"
Xsi:schemalocation = "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
<! -You also need to introduce the following 3 resource files on CXF, which are in Cxf.jar-->
< import resource = "Classpath:meta-inf/cxf/cxf.xml"/>
< import resource = "Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
< import resource = "Classpath:meta-inf/cxf/cxf-servlet.xml"/>
...... ...... ...... ...... ...... ...... ...... ...... ...... ......
</bean>

The second is to configure WebService in the spring's configuration file, as follows:
< Jaxws:endpoint ID = "Usermanager" address = "/usermanager"
Implementorclass = "Com.cxf.service.UserService" >
< Jaxws:implementor >
< Bean id = "Userserviceimpl"
class = "Com.cxf.service.impl.UserServiceImpl" >
< property name = "Userdao" >
< ref bean = "Userdao"/>
</Property >
</Bean >
</jaxws:implementor >
</jaxws:endpoint >

Attention:
Address for WebService issued by ①
②, Implementorclass interface implemented for the WebService
③, < Jaxws:implementor ></Jaxws:implementor > Define the implementation class for the specified interface Implementorclass

Also, after configuration is complete in spring's configuration file, you need to modify the Web.xml file
< servlet >
< Servlet-name > Cxfservlet </servlet-name >
< Servlet-class >
Org.apache.cxf.transport.servlet.CXFServlet
</Servlet-class >
</servlet >
< servlet-mapping >
< Servlet-name > Cxfservlet </servlet-name >
< Url-pattern >/services/* </url-pattern >
</servlet-mapping >
Note <url-pattern>/Services/*</url-pattern> configuration, Cxfservlet will intercept such URLs for processing. The webservice configured above will be accessed via the following URL:
Http://localhost:8080/cxf-ws/services/UserManager
Usermanager is the value for the Address property in the < Jaxws:endpoint > tab
Cxf-ws is the name of this project

After the configuration is complete, deploy the project to Tomcat
Enter Url:http://localhost:8080/cxf-ws/services/usermanager? wsdl
You will see the generated WSDL file

To enable direct access to the Insert method in Com.cxf.service.UserService, test the following:
Create a insertuser.html page:
< HTML >
< head >
< script type = "Text/javascript" >
Function post () {
var xmlhttp= false;
if (!xmlhttp && typeof xmlhttprequest!= ' undefined ') {
try {
XMLHTTP = new XMLHttpRequest ();
catch (e) {
Xmlhttp= false;
}
}
if (!xmlhttp && window.createrequest) {
try {
XMLHTTP = Window.createrequest ();
catch (e) {
Xmlhttp= false;
}
}
var dest = document.getElementById (' destination '). Value;
Xmlhttp.open ("POST", dest, True);
xmlhttp.onreadystatechange= function () {
if (xmlhttp.readystate==4) {
document.getElementById ("Result"). InnerHTML = Xmlhttp.responsetext;
}
}
Xmlhttp.setrequestheader ("Man", "POST" + dest + "http/1.1")
Xmlhttp.setrequestheader ("MessageType", "call")
Xmlhttp.setrequestheader ("Content-type", "Text/xml");
var texta = document.getElementById (' texta ');
var soapmess = Texta.value;
Xmlhttp.send (soapmess);
}
</script >
< BODY >
Hi there
< form ID = "forma" >
< textarea id = "texta" rows = "ten" cols = "No" >
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<soap-env:envelope xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"
Xmlns:xsi= "Http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/1999/XMLSchema" >
<SOAP-ENV:Body>
< I nsert>
<userId>2005221104210066</userId>
<username>leon cao</username>
<userEmail>caohongliang2@163.com</userEmail>
<userAge>23</userAge>
</I nsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</textarea >
< p >
< input type = "text" id = "destination" size = "value" = "Http://localhost:8080/cxf-ws/services/UserManager" ></input >
< p >
< input type = "button" onclick = "post ()" name = "Submit" value = "Submit" ></input >
</form >
< div id = "container" ></div >
Result:
< hr >
< div id = "result" >
</div >
< hr >
< div id = "Soap" >
</div >
</Body >
</HTML >
In this test page you need to notice the section marked red, as follows is a SOAP message:
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<soap-env:envelope xmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"
Xmlns:xsi= "Http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/1999/XMLSchema" >
<SOAP-ENV:Body>
< I nsert>
<userId>2005221104210066</userId>
<username>leon cao</username>
<userEmail>caohongliang2@163.com</userEmail>
<userAge>23</userAge>
</I nsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Attention:
①<soap-env:body > tags < I nsert> corresponding @WebMethod (operationname= "Insert")
The value of the OperationName property in
②<userid>, <userName>, <userEmail>, <userAge> tag pairs each corresponding to the Insert method
The value of the Name property in the @WebParam:
@WebParam (name = "UserId") String userId,
@WebParam (name = "UserName") String UserName,
@WebParam (name = "UserEmail") String UserEmail,
@WebParam (name = "Userage") int userage);
When the SOAP message is sent to the WebService, the SOAP message is parsed automatically, the insert resolves to the Insert method, and the <userId>, <userName>, <userEmail> , <userAge> label to the value between the resolution of the corresponding parameters UserID, username, useremail, userage
③< input type = "text" id = "destination" size = "VALUE =" Http://localhost:8080/cxf-ws/services/UserManager "></input >
The Value property in the text label is the address of the WebService

Run this page, click Submit will insert a row of data in the database, that is, called the Inser method succeeded

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.