WebService Helloword (Java) RS

Source: Internet
Author: User
Tags log4j

WebService RS (HelloWorld)

1.pom.xml file

<dependencies> <!--using CXF RS Development--<dependency><groupId>org.apache.cxf</groupId><  Artifactid>cxf-rt-frontend-jaxrs</artifactid><version>3.0.1</version></dependency> <!--built-in jetty Web server--><dependency><groupid>org.apache.cxf</groupid><artifactid> cxf-rt-transports-http-jetty</artifactid><version>3.0.1</version></dependency><!-- Implementing--><dependency><groupid>org.slf4j</groupid><artifactid>slf4j-log4j12< using the log4j log /artifactid><version>1.7.12</version></dependency><!--in the CXF extension provider, which provides the conversion JSON interface-->< dependency><groupid>org.apache.cxf</groupid><artifactid>cxf-rt-rs-extension-providers< /artifactid><version>3.0.1</version></dependency><!--CXF extension provider convert JSON default requirements a toolkit-->< Dependency><groupid>org.codehaus.jettison</groupid><artifactid>jettison</artifacTid><version>1.3.7</version></dependency> </dependencies> <build><plugins > <plugin> <groupId>org.apache.maven.plugins</groupId> <a Rtifactid>maven-compiler-plugin</artifactid> <version>2.3.2</version> & Lt;configuration> <source>1.7</source> &LT;TARGET&GT;1.7&LT;/TARGET&G                T </configuration> </plugin> </plugins></build>

2. Create a service interface

Package Com.baidu.service;import Javax.ws.rs.consumes;import Javax.ws.rs.delete;import javax.ws.rs.GET;import Javax.ws.rs.post;import Javax.ws.rs.put;import Javax.ws.rs.path;import Javax.ws.rs.pathparam;import Javax.ws.rs.produces;import javax.ws.rs.queryparam;import Com.baidu.domain.User; @Path ("/userservice")//server access resource path  @Produces ("*/*")//The return value of the Generate method//Specifies whether the generated format data is returned to the client public interface UserService {//@GET query @PUT modification @POST increased @DELETE Delete the @post @Path ("/saveuser") @Consumes ({"Application/xml", "Application/json"})//The parameters of the consumption method public void Saveuser (user user @DELETE @path ("/deleteuser") @Consumes ({"Application/xml", "Application/json"}) public void DeleteUser (@QueryParam (value= "id") Integer ID), @PUT @path ("/updateuser") @Consumes ({"Application/xml", "Application/json"}) public void UpdateUser (User User), @GET @path ("/queryuser/{id}") @Consumes ({"Application/xml", "Application/json"}) @Produces ({"Application/xml "," Application/json "}) public User Queryuser (@PathParam (value=" id ") Integer ID);}

@Path Server Access Resource Path
@Produces Specifies the amount of data format generated in that format to return to the client
@Consumes Specify the data format to receive
@POST Specifies that the method is saved (saved)
@DELETE Specifies that the method is deleted
@PUT Specifies that the method is modified
@GET Specifies that the method is a query

  

3. Create a Service implementation class

Package Com.baidu.service.imp;import Com.baidu.domain.user;import Com.baidu.service.userservice;public class Userserviceimp implements UserService {public void Saveuser (user user) {System.out.println ("Saveuser" +user);} public void DeleteUser (Integer id) {SYSTEM.OUT.PRINTLN ("DeleteUser ID:" +id);} public void UpdateUser (user user) {System.out.println ("UpdateUser User:" +user); Public User Queryuser (Integer id) {SYSTEM.OUT.PRINTLN ("id"), if (id==1) {System.out.println (ID); User U=new User (); U.setid (ID); U.setname ("Zhangsan"); return u;} return null;}}

4. Publishing services

Package Com.baidu.publish;import Org.apache.cxf.jaxrs.jaxrsserverfactorybean;import Com.baidu.domain.user;import Com.baidu.service.userservice;import Com.baidu.service.imp.userserviceimp;public class PublishService {public static void Main (string[] args) {userservice us=new userserviceimp (); Jaxrsserverfactorybean serverfactorybean=new Jaxrsserverfactorybean (); Serverfactorybean.setresourceclasses ( User.class); Serverfactorybean.setservicebean (US), serverfactorybean.setaddress ("http://localhost:8099"); Serverfactorybean.create ();}}

Pom.xml file for test project

 <dependencies> <!--using CXF RS Development--<dependency><groupId>org.apache.cxf</groupId>< Artifactid>cxf-rt-frontend-jaxrs</artifactid><version>3.0.1</version></dependency> <!--using log4j logs for--><dependency><groupid>org.slf4j</groupid><artifactid> slf4j-log4j12</artifactid><version>1.7.12</version></dependency><!--Use RS Client-- <dependency><groupid>org.apache.cxf</groupid><artifactid>cxf-rt-rs-client</ artifactid><version>3.0.1</version></dependency><!--in the CXF extension provider, which provides the conversion JSON interface-->< dependency><groupid>org.apache.cxf</groupid><artifactid>cxf-rt-rs-extension-providers< /artifactid><version>3.0.1</version></dependency><!--CXF extension provider convert JSON default requirements a toolkit-->< Dependency><groupid>org.codehaus.jettison</groupid><artifactid>jettison</artifactid ><version>1.3.7</version></dependency> </dependencies> <build><plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> &LT;ARTIFACTID&GT;MAVEN-COMPILER-PL                    Ugin</artifactid> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configu Ration> </plugin> </plugins></build>

Testing a published Service

public static void Main (string[] args) {//save//USER user = new user ();//User.setid (1);//User.setname ("Zhangsan");//Web Client.create ("Http://localhost:8099/userService/saveUser")//. Type (Mediatype.application_xml). Post (user);//delete/ /Webclient.create ("http://localhost:8099/userService/deleteUser?id=1"). Type (mediatype.application_xml). Delete ( );//modify//USER user = new user ()//User.setid (1);//User.setname ("Zhangsan");//Webclient.create ("http://localhost : 8099/userservice/deleteuser "). Type (Mediatype.application_xml). put (user);/  /query//user User =webclient.create ( "HTTP://LOCALHOST:8099/USERSERVICE/QUERYUSER/1")//. Accept (Mediatype.application_xml). get (User.class);// SYSTEM.OUT.PRINTLN (user);}

User class

Package Com.baidu.domain;import Javax.xml.bind.annotation.xmlelement;import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name= "User") public class User {private Integer id;private String name;public Integer getId () {return Id;} public void SetId (Integer ID) {id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "User [id=" + Id + ", name=" + name + "]";}}

  

WebService Helloword (Java) RS

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.