Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of Java WebService Open Source Framework CXF

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "Java WebService open source framework CXF case analysis". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Introduction to CXF

CXF is an open source WebService framework. Apache CXF = Celtix + XFire, called Apache CeltiXfire at first, then changed its name to Apache CXF, hereinafter referred to as CXF. CXF inherits the essence of Celtix and XFire open source projects, provides comprehensive support for JAX-WS, and provides a variety of Binding, DataBinding, Transport and a variety of Format support, and according to the needs of the actual project, the use of code first (Code First) or WSDL first (WSDL First) to easily achieve the release and use of Web Services.

Support multiple standards

Support for JAX-WS, JAX-WSA, JSR-181 and SAAJ

Support for SOAP 1.1,1.2, WS-I BasicProfile, WS-Security, WS-Addressing, WS-RM and WS-Policy

Support for WSDL 1.1,2.0

Support for MTOM

It supports multiple protocols, such as SOAP1.1,1,2, XML/HTTP, RESTful HTTP, or CORBA. CORBA (Common Object Request Broker Architecture common object request broker architecture, WS used in earlier languages. C #) C #)

Cxf is based on SOA bus structure, relies on spring to complete the module integration, and realizes the SOA mode.

Flexible deployment: can be run with Tomcat,Jboss,Jetty (built-in), weblogic above.

Getting started with CXF

Let's also take yesterday's weather service as an example to take a look at the development process of CXF.

The implementation of the server 1. Create an empty java project, create a lib directory, put all the jar packages in the lib directory, and then introduce the jar package for the project, select build path, and then Add JARS, just select cxf-manifest.jar.

two。 To create a SEI interface, you need to add @ WebService annotation @ WebServicepublic interface WeatherInterface {public String queryWeather (String cityName);} 3. Create SEI interface implementation class public class WeatherInterfaceImpl implements WeatherInterface {public String queryWeather (String cityName) {if ("Henan" .equals (cityName)) {return "thermal explosion";} else {return "hail";}} 4. Publish service public class WeatherServer {public static void main (String [] args) {/ / create service factory Bean JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean (); / set service interface jaxWsServerFactoryBean.setServiceClass (WeatherInterface.class); / / set service implementation class jaxWsServerFactoryBean.setServiceBean (new WeatherInterfaceImpl ()) / / set the service address jaxWsServerFactoryBean.setAddress ("http://127.0.0.1:12345/weather"); / / create service jaxWsServerFactoryBean.create ();}} 5. Access the wsdl file address of the service to see whether the service has been released successfully, http://127.0.0.1:12345/weather?wsdl.

The server that publishes SOAP1.2

SOAP is divided into version 1.1 and version 1.2. JDK1.6 does not support 1.2. we can publish the server side of SOAP1.2 through CXF.

You just need to add the annotation @ BindingType (SOAPBinding.SOAP12HTTP_BINDING) to the interface. Then republish the service.

Import javax.jws.WebService;import javax.xml.ws.BindingType;import javax.xml.ws.soap.SOAPBinding;@WebService@BindingType (SOAPBinding.SOAP12HTTP_BINDING) public interface WeatherInterface {public String queryWeather (String cityName) } client implementation Wsdl2java command is a tool for generating client side provided by CXF. Similar to wsimport, it can generate client code Wsdl2java common parameter:-d, specify output directory-p, and specify package name according to WSDL. If this parameter is not specified, the default package name is the reverse Wsdl2java of WSDL namespace supports SOAP1.1 and SOAP1.21. We first create a client-side project, and then introduce the jar package. As above, we can select cxf-manifest.jar using Add JARS, and then use the tool to generate client-side wsdl2java-p com.cad.cxf-d. Http://127.0.0.1:12345/weather?wsdl

two。 Create client public class WeatherClient {public static void main (String [] args) {JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean (); / set service interface jaxWsProxyFactoryBean.setServiceClass (WeatherInterface.class); / / set service address jaxWsProxyFactoryBean.setAddress ("http://127.0.0.1:12345/weather"); / / get service interface instance WeatherInterface weatherInterface= (WeatherInterface) jaxWsProxyFactoryBean.create ()) / / call method String message=weatherInterface.queryWeather ("Henan"); System.out.println (message);}} CXF+Spring integrates and publishes the server-side implementation of the SOAP pattern. Create a WEB project and import jar package 2. Create the SEI interface @ WebService@BindingType (SOAPBinding.SOAP12HTTP_BINDING) public interface WeatherInterface {public String queryWeather (String cityName);} 3. Create the SEI implementation class public class WeatherInterfaceImpl implements WeatherInterface {public String queryWeather (String cityName) {if ("Henan" .equals (cityName)) {return "thermal explosion";} else {return "hail";}} 4. Configure spring configuration file, applicationContext.xml 5. Configure web.xml CXFSpringDemo / / configure Tomcat load Spring configuration file contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener / / configure Servlet CXF org.apache.cxf.transport.servlet.CXFServlet CXF / ws/* index.html index.htm index.jsp default.html default.htm default.jsp 6 provided by CXF. Deploy to Tomcat, publish services, and access http://localhost:8080/CXFSpringDemo/ws/weather?wsdl

The implementation of the client 1. Create the project, import the jar package, and generate the client wsdl2java-p com.cad.cxf-d. Http://localhost:8080/CXFSpringDemo/ws/weather?wsdl

two。 Configure the Spring file 3. Get the service implementation class through the Spring container and call the method public class WeatherClient {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml"); WeatherInterface weatherInterface = (WeatherInterface) context.getBean ("weatherClient"); String message=weatherInterface.queryWeather ("Henan"); System.out.println (message);}}

CXF publishes services in REST mode

REST, which means declarative state transfer (Representational State Transfer, REST), is a kind of software architecture style.

Because the Web service of REST mode is obviously more concise than the complex SOAP and XML-RPC, more and more web services begin to use REST style to design and implement rest services using HTTP as the transport protocol. REST uses HTTP to achieve accurate resource location.

For example: non-rest method: http://ip:port/queryUser.action?userType=student&id=001 Rest method: http://ip:port/user/student/query/0011. Create a project and import CXF jar package 2. 0. Create an entity class Student @ XmlRootElement (name= "student") to implement the conversion between XML and object. The name attribute specifies the root element @ XmlRootElement (name= "student") public class Student {private int id; private String name; private Date birthday; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} public Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this.birthday = birthday;}} 3. To create a SEI interface @ WebService//@Path ("/ student") is to specify the path to access the interface @ Path ("/ Student") public interface StudentInterface {/ / specify the request method. If the server publishes GET (POST), the client must use GET (POST @ GET / /) to specify the service data type, which can be XML. Data types such as json @ Produces (MediaType.APPLICATION_XML) / / @ Path ("/ query/ {id}") specify the path of the method. "{id}" refers to parameters, multiple parameters, separated by "/", in "{}" @ Path ("/ query/ {id}") public Student queryStudent (@ PathParam ("id") int id) GET @ Produces (MediaType.APPLICATION_XML) @ Path ("/ queryList/ {name}") public List queryList (@ PathParam ("name") String name);} 4. Create the SEI implementation class public class StudentInterfaceImpl implements StudentInterface {@ Override public Student queryStudent (int id) {Student s=new Student (); s.setId (666); s.setName ("Zhang San"); s.setBirthday (new Date ()); return s;} @ Override public List queryList (String name) {Student s=new Student (); s.setId S.setName (Zhang San); s.setBirthday (new Date ()); Student s2=new Student (); s2.setId (888); s2.setName (Li Si); s2.setBirthday (new Date ()); List l=new ArrayList (); l.add (s); l.add (S2); return 1;}} 5. Publishing service public class StudentServer {public static void main (String [] args) {JAXRSServerFactoryBean jaxrsServerFactoryBean=new JAXRSServerFactoryBean (); / / set the service implementation class jaxrsServerFactoryBean.setServiceBean (new StudentInterfaceImpl ()) / / set the resource class. If there are multiple resource classes, you can separate them with ",". For example, Student.class StudentInterface.class is a resource class, but Student.class StudentInterface.class is already included in StudentInterfaceImpl, so there is no need to specify jaxrsServerFactoryBean.setResourceClasses (StudentInterfaceImpl.class) repeatedly; / / set the service address jaxrsServerFactoryBean.setAddress ("http://127.0.0.1:12345/Class");") / / publish service jaxrsServerFactoryBean.create ();}} 6. Test service accesses query method http://127.0.0.1:12345/Class/Student/query/001

Access queryList method http://127.0.0.1:12345/Class/Student/queryList/xxx

If GET (POST) is specified when the server publishes the request, the client must use GET (POST) to access the server, otherwise an exception will be reported.

If both XML and JSON media types are specified on the same method, XML is returned by default under GET request, and JSON is returned by default under POST request

CXF+Spring integrates and publishes services of REST mode

Comprehensive case: mobile phone attribution query

This is the end of the "Java WebService Open Source Framework CXF case study". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report