WEBSERVICE--CXF and spring combine to release restfull-style services

Source: Internet
Author: User

Here is an example of publishing a restfull style:

Service side:

Entity:

Package entity;
Import Java.util.date;import javax.xml.bind.annotation.xmlrootelement;/** * entity */@XmlRootElement (name= "student") pub    Lic class Pojo {private long ID;    temperature private String detail;    Date the private date date;    highest private int temperature_max;            Minimum private int temperature_min;    Public long GetId () {return id;    } public void SetId (long id) {this.id = ID;    } public String Getdetail () {return detail;    } public void Setdetail (String detail) {this.detail = detail;    Public Date getDate () {return date;    public void SetDate (date date) {this.date = date;    } public int Gettemperature_max () {return temperature_max;    } public void Settemperature_max (int temperature_max) {This.temperature_max = Temperature_max;    } public int Gettemperature_min () {return temperature_min; } public void Settemperature_min (int temperature_min) {This.teMperature_min = Temperature_min; }        }

Be sure to add annotation to the front of the entity class in order for the information to be converted between Pojo and XML

Sei:

Package Service;import Java.util.list;import Javax.jws.webparam;import javax.jws.webresult;import Javax.jws.webservice;import Javax.ws.rs.consumes;import Javax.ws.rs.get;import Javax.ws.rs.Path;import Javax.ws.rs.pathparam;import Javax.ws.rs.produces;import javax.ws.rs.core.mediatype;import entity. pojo;/** * Interface */@WebService @path ("/student") public interface Weatherinterface {    //mediatype        //Query student list    @ GET    @Path ("/querylist/{type}")    @Consumes (mediatype.application_xml) public    list<pojo> Queryweather (@PathParam ("type") String cityname);            Query student    @GET    @Path ("/query/{id}")    @Produces ({mediatype.application_json,mediatype.application_xml }) Public    Pojo Querybyid (@PathParam ("id") long ID);    }

Before each method, use annotation to declare the method type of the HTTP request, such as Get,delete,post, PUT

The ID in the @Path ("/room/{id}") is a parameter and should be declared in the parameter list of the method: public Pojo Querybyid (@PathParam ("id") long ID)

Implementation class:

Package Service;import Java.util.arraylist;import java.util.calendar;import java.util.date;import java.util.List; Import Javax.jws.webmethod;import javax.jws.webparam;import Javax.jws.webresult;import Javax.jws.WebService;import Com.sun.org.glassfish.gmbal.parameternames;import entity. pojo;/** * Implementation Class */public classes Impl implements Weatherinterface {@Override public list<pojo> queryweather (stri                ng CityName) {list<pojo> List = new arraylist<pojo> ();        Calendar Attachment Calendars calendar = Calendar.getinstance ();                int day = Calendar.get (calendar.date);        Pojo pojo1 = new Pojo ();        Pojo1.setid (01);        Pojo1.setdetail ("Clear 1");        Pojo1.setdate (New Date ());        Pojo1.settemperature_max (5);                Pojo1.settemperature_min (-6);        Pojo Pojo2 = new Pojo ();        Pojo2.setid (02);        Pojo2.setdetail ("Clear 2");        Calendar.set (Calendar.date, day+1);        Pojo2.setdate (Calendar.gettime ()); Pojo2.settemperature_max (5);                Pojo2.settemperature_min (-6);        Pojo Pojo3 = new Pojo ();        Pojo3.setid (003);        Pojo3.setdetail ("Clear 3");        Calendar.set (Calendar.date, day+2);        Pojo3.setdate (Calendar.gettime ());        Pojo3.settemperature_max (5);                        Pojo3.settemperature_min (-6);        List.add (POJO1);        List.add (POJO2);                List.add (POJO3);    return list;        } @Override Public Pojo Querybyid (long id) {Pojo pojo1 = new Pojo ();        Pojo1.setid (ID);        Pojo1.setdetail ("Zhang San");        Pojo1.setdate (New Date ());        Pojo1.settemperature_max (5);                Pojo1.settemperature_min (-6);    return pojo1; }}

Spring configuration file:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:jaxrs= "Http://cxf.apache.org/jaxrs" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans H            Ttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context             Http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://cxf.apache.org/jaxrs Http://cxf.apache.org/schemas/jaxrs.xsd "> <!--Service--<bean id=" Weatherinterface " class= "service. Impl "/> <!--to configure WebService--<jaxrs:server address="/weather "> <jaxr by Jaxrs:server Way s:servicebeans> <ref bean= "Weatherinterface"/> </jaxrs:serviceBeans> </jaxrs:serve   R></beans>             

XML configuration file:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" XM Lns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:/ /java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name></display-name> <welcome-file-list > <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--loading Spring containers--<con Text-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:a pplicationcontext.xml</param-value> </context-param> <listener> <listener-class& Gt;org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!--cxf SE Rvlet-<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>o Rg.apache.cxf.transport.servleT.cxfservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*< /url-pattern> </servlet-mapping> </web-app>

Deploy the project to Tomcat, address bar input: http://localhost:8989/cxf_rest_spring_service/ws/

WEBSERVICE--CXF and spring combine to release restfull-style services

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.