SPRINGMVC + Apache wink Rest Integration

Source: Internet
Author: User

Apache Wink is a Java-pure rest framework. It fully implements the JSR 311 and extends some of its functionality, in addition to providing good extensibility, and is invaluable for seamlessly integrating with the popular Java framework Spring .

1. Load the packages used by the Apache Wink and spring consolidation via maven:
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-common</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-server</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-spring-support</artifactId>
<version>1.4</version>
</dependency>

2. Configuration

Web service is, of course, a Web application, so the portal is a servlet, which is configured in XML, and the rest access is arranged for wink to handle:

<servlet>
<servlet-name>restService</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<!--
<init-param>
<param-name>applicationConfigLocation</param-name>
<param-value>/WEB-INF/resources/resource.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>restService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Integration with spring, need to speak wink package of wink-core-context.xml load , configured as follows

<context-param>

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

<param-value>classpath:meta-inf/server/wink-core-context.xml,classpath:config/spring/spring-config.xml </param-value>

</context-param>

Spring configuration-related resource:

<bean class= "Org.apache.wink.spring.Registrar" >
<property name= "Instances" >
<set>
<ref local= "Userserviceresource"/>
</set>
</property>
</bean>
<bean id= "Userserviceresource" class= "Com.xxx.cms.controller.UserServiceResource"/>

..... The definition behind is the resrouce definition that everyone is more familiar with.

Test code:

/************************************* Public Connection Category ****************************************/
/**
* Add a public connection category
* @param jsonobj Public Connection classification JSON data
* @return 0: Failure 1: Success 2: Repeat add
* @throws Unsupportedencodingexception exception
*/
@PUT
@Path ("/add/commonlinktype")
@Produces ({Mediatype.application_json})
@AuthMethod
Public Jsonobject Addcommonlinktype (Jsonobject jsonobj) throws unsupportedencodingexception{
Jsonobject result = new Jsonobject ();
0 = Add failed
Result.put ("result", 0);
if (null! = Jsonobj) {

Commonlinktype Commonlinktype = new Commonlinktype ();
Buildjsonobjecttocommonlinktype (Commonlinktype,jsonobj);
Whether the connection already exists
if (Hasexistcommonlinktype (Commonlinktype)) {
2 indicates existence
return result.put ("result", 2);
}
Boolean success = Commonlinkservice.addcommonlinktype (Commonlinktype);
if (success) {
1 indicates a successful addition
Result.put ("Result", 1);
return result;
}
}
return result;
}

/**
* Delete Public Connection categories
* @param ID Category ID
* @return 0: Failure 1: Success
*/
@DELETE
@Path ("/delete/commonlinktype/{id}")
@Produces ({Mediatype.application_json})
Public Jsonobject Deletecommonlinktype (@PathParam ("id") String ID) {
HttpSession session = Request.getsession ();
User user = (user) Session.getattribute ("Loginuser");
Jsonobject result = new Jsonobject ();
Result.put ("result", 0);
if (null! = user) {
String userId = string.valueof (User.getid ());
Commonlinktype Commonlinktype = new Commonlinktype (Id,userid);
Boolean success = Commonlinkservice.deletecommonlinktype (Commonlinktype);
if (success) {
Result.put ("Result", 1);
return result;
}
}
return result;
}

/**
* Update Public link category
* @param jsonobj Public Connection classification JSON data
* @return 0: Failure 1: Success
*/
@POST
@Path ("/update/commonlinktype")
@Produces ({Mediatype.application_json})
Public Jsonobject Updatecommonlinktype (Jsonobject jsonobj) {
HttpSession session = Request.getsession ();
User user = (user) Session.getattribute ("Loginuser");
Jsonobject result = new Jsonobject ();
Result.put ("result", 0);
if (null! = Jsonobj && null! = user) {
String id = jsonobj.optstring ("id");
String userId = string.valueof (User.getid ());
String name = jsonobj.optstring ("name");
Commonlinktype Commonlinktype = new Commonlinktype (Id,name,userid,datautil.date2str (New Date ()));
Boolean success = Commonlinkservice.updatecommonlinktype (Commonlinktype);
if (success) {
Result.put ("Result", 1);
return result;
}
}
return result;
}

/**
* Query Public connection Category list
* @param page now
* @param pagenum number of bars per page
* @return Common connection Category list JSON
*/
@GET
@Path ("/list/commonlinktype")
@Produces ({Mediatype.application_json})
Public Jsonobject findcommonlinktypelist (@DefaultValue ("1") @QueryParam ("page") int page, @DefaultValue ("10") @ Queryparam ("pagenum") int pagenum) {
Jsonobject result = new Jsonobject ();
Jsonarray Jsonarray = new Jsonarray ();
Result.put ("Count", 0);
Result.put ("Commonlinktypelist", Jsonarray);
HttpSession session = Request.getsession ();
User user = (user) Session.getattribute ("Loginuser");
if (null! = user) {
PageBounds PB = Pageboundsutil.pageboundsorderextend ("Orderid.desc");
Pb.setpage (page);
Pb.setlimit (Pagenum);
Commonlinktype Commonlinktype = new Commonlinktype (string.valueof (User.getid ()));
pager<commonlinktype> commonlinkpage = commonlinkservice.findcommonlinktypelist (COMMONLINKTYPE,PB);
if (null! = Commonlinkpage && commonlinkpage.gettotal () > 0) {
Result.put ("Count", Commonlinkpage.gettotal ());
Buildcommonlinktypelisttojsonaarray (Commonlinkpage,jsonarray);
}
}
return result;
}

/**
* Determine if the current classification already exists
* @param commonlinktype Classification parameters
* @return true: Indicates the existence of false: Indicates that there is no
*/
Private Boolean Hasexistcommonlinktype (Commonlinktype commonlinktype) {
int count = Commonlinkservice.findcommonlinktypecountbyname (Commonlinktype);
if (Count > 0) {
return true;
}
return false;
}

private void Buildjsonobjecttocommonlinktype (Commonlinktype commonlinktype, Jsonobject jsonobj) throws unsupportedencodingexception {
HttpSession session = Request.getsession ();
User user = (user) Session.getattribute ("Loginuser");
Commonlinktype.setid (Utils.getuuid ());
Commonlinktype.setname (Urldecoder.decode (jsonobj.optstring ("name"), "UTF-8"));
Commonlinktype.setuserid (null! = user?) String.valueof (User.getid ()): "");
Commonlinktype.setcreatedate (Datautil.date2str (New Date ()));
Commonlinktype.setlastmodifydate (Datautil.date2str (New Date ()));
}

private void Buildcommonlinktypelisttojsonaarray (pager<commonlinktype> commonlinktypepage,jsonarray result) {
if (null! = Commonlinktypepage) {
For (Commonlinktype CommonlinkType:commonlinkTypePage.getDatas ()) {
Jsonobject jsobj = new Jsonobject ();
Jsobj.put ("id", Commonlinktype.getid ());
Jsobj.put ("Name", Commonlinktype.getname ());
Jsobj.put ("UserId", Commonlinktype.getuserid ());
Jsobj.put ("CreateDate", Commonlinktype.getcreatedate ());
Jsobj.put ("Lastmodifydate", Commonlinktype.getlastmodifydate ());
Result.put (Jsobj);
}
}
}

Private Jsonobject buildcommonlinktypejsonobj (jsonobject jsobj,commonlinktype commonlinktype) {
Jsobj.put ("id", Commonlinktype.getid ());
Jsobj.put ("Name", Commonlinktype.getname ());
Jsobj.put ("UserId", Commonlinktype.getuserid ());
Jsobj.put ("CreateDate", Commonlinktype.getcreatedate ());
Jsobj.put ("Lastmodifydate", Commonlinktype.getlastmodifydate ());
return jsobj;
}

Private Jsonobject buildcommonlinkjsonobj (jsonobject jsobj,commonlink commonlink) {
Jsobj.put ("id", Commonlink.getid ());
Jsobj.put ("Name", Commonlink.getname ());
Jsobj.put ("UserId", Commonlink.getuserid ());
Jsobj.put ("CreateDate", Commonlink.getcreatedate ());
Jsobj.put ("Lastmodifydate", Commonlink.getlastmodifydate ());
Jsobj.put ("url", Commonlink.geturl ());
Jsobj.put ("Position", commonlink.getposition ());
return jsobj;
}

Attached to the rest service test interface, you can definitely use the app:

You need the source code please contact buckle: 3121026417

SPRINGMVC + Apache wink Rest Integration

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.