Resteasy usage example

Source: Internet
Author: User
Tags setcell jboss application server


RESTEasy introduction:

RESTEasy is an open-source project of JBoss. It provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a complete implementation of JAX-RS specifications and passes JCP certification. As a JBOSS project, it can be well integrated with the JBOSS application server. However, it can also run in any Servlet container that runs JDK 5 or a later version. RESTEasy also provides a RESTEasy JAX-RS client call framework. It can be easily integrated with EJB, Seam, Guice, Spring and Spring MVC. Supports automatic GZIP decompression on the client and server.
Standard content
The JAX-RS provides some annotations that encapsulate a resource class, a POJOJava class, into Web resources.
Annotations include:
@ Path: indicates the relative Path of the Resource class or method.
@ GET, @ PUT, @ POST, @ DELETE. The annotation method is the type of the HTTP request used.
@ Produces: indicates the MIME media type returned.
@ Consumes: indicates the MIME media type that can accept the request.
@ PathParam, @ QueryParam, @ HeaderParam, @ CookieParam, @ MatrixParam, @ FormParam, respectively indicate that the parameters of the method come from different locations of the HTTP request. For example, @ PathParam comes from the URL path, @ QueryParam is the URL query parameter, @ HeaderParam is the header information of the HTTP request, and @ CookieParam is the Cookie of the HTTP request.

The following uses the iplat4j + Spring + ibatiS architecture platform of Baoxin software and a simple case column of the MiniUI front-end technology to explain the deletion operation.
1. Configure web. xml

<Servlet>
<Servlet-name> Resteasy </servlet-name>
<Servlet-class>
Org. jboss. resteasy. plugins. server. servlet. HttpServletDispatcher
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> Resteasy </servlet-name>
<Url-pattern>/restservice/* </url-pattern>
</Servlet-mapping>

Second: write the Rest *. java class
/**
* Data subscription Rest
* @ Author Administrator
*
*/
@ SuppressWarnings (value = {"rawtypes "})
@ Path ("restservice ")
Public class RestSubscribe {
Logger logger = Logger. getLogger (RestUser. class );
@ POST
@ Path ("/deleteSubscribe2 ")
@ Produces ("application/json; charset = UTF-8 ")
/**
* Delete A data subscription
* Delete Based on consumerid and categoryId
* @ Param request
* @ Return
*/
Public String deleteSubscribe2 (@ Context HttpServletRequest request ){
EiInfo inInfo = new EiInfo ();
String data = request. getParameter ("data ");
String consumerId = request. getParameter ("consumerId ");
String operator = request. getParameter ("operator ");
InInfo. setCell ("I", 0, "consumerId", consumerId );
InInfo. setCell ("I", 0, "operator", operator );
InInfo. setCell ("I", 0, "categoryIds", data );
InInfo. set (EiConstant. serviceName, "FBSubscribe ");
InInfo. set (EiConstant. methodName, "deleteSubscribe2 ");
InInfo = SoaManager. callNewTx (inInfo );
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("result", "deleted successfully ");
Return JSONUtil. getInstance ().
Object2JSON (map,
JSONUtil. Feature. DEFAULT_DATE_PATTERN );
}
}


Third: service class Compilation
/**
* Data subscription service
* @ Author Administrator
*
*/
@ SuppressWarnings (value = {"rawtypes", "unchecked "})
Public class ServiceFBSubscribe extends ServiceBase {
Logger logger = Logger. getLogger (ServiceFBSubscribe. class );
SubscribeRepository subscribeRep = (SubscribeRepository) SpringApplicationContext. getBean ("subscribeRep ");

/**
* According to the consumerId and categoryId
* Delete A data subscription
* @ Param user
*/
Public EiInfo deleteSubscribe2 (EiInfo inInfo ){
Map <String, String> resultMap = new HashMap <String, String> ();
Try {
String categoryId = (String) inInfo. getCell ("I", 0, "categoryIds ");
String consumerId = (String) inInfo. getCell ("I", 0, "consumerId ");
String operator = (String) inInfo. getCell ("I", 0, "operator ");
String [] categoryIds = categoryId. split (",");
Subscribe subscribe = new Subscribe ();
For (String id: categoryIds ){
Subscribe. setCategoryId (id );
Subscribe. setConsumerId (consumerId );
Subscribe. setGuid (id );
SubscribeRep. delete2 (subscribe );
}
// Print the status returned by the server

ResultMap. put ("message ",
"Deleted ");
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
InInfo. setStatus (EiConstant. STATUS_FAILURE );
InInfo. setMsg ("failed to delete data subscription! ");
ResultMap. put ("result", "false ");
ResultMap. put ("message", "failed to delete data subscription! ");
}
Return inInfo;
}
}

Fourth: Write the management warehouse class (* Repository. java)
1. interface class:
Public interface SubscribeRepository {
/**
* Delete data push feedback
* @ Param user
*/
Public void delete2 (Subscribe subscribe );
}
2. Implementation class:
/**
* Data subscription and maintenance warehousing
* @ Author: hushiguo
* @ Date: 2:59:15 pm
*/
@ SuppressWarnings ({"unchecked", "rawtypes", "unused "})
Public class SubscribeRepositoryImpl extends RepositoryBase implements SubscribeRepository {

Public void delete2 (Subscribe subscribe ){
// TODO Auto-generated method stub
If (subscribe. getConsumerId () = null | "". equals (subscribe. getConsumerId ()))
Throw new AppException ("Data subscription does not exist! ");
Try {
DaoUtil. delete (dao, "FBSubscribe. delete2", subscribe );
} Catch (Exception e ){
Throw new AppException ("failed to delete data subscription! ");
}
}
}
Fifth: spring registered Storage Class (applicationContext-fb-persist.xml)
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<Bean id = "subscribeRep" class = "com. baosight. buap. fb. persist. SubscribeRepositoryImpl">
<Property name = "dao" ref = "dao"/>
</Bean>
</Beans>
Sixth: resteasy. properties)
Com. baosight. buap. fb. rest. RestSubscribe
7. Compile * SQL. xml of ibatIS (FBSubscribe. xml)
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE sqlMap PUBLIC "-// iBATIS.com//DTD SQL Map 2.0 //" http://www.ibatis.com/dtd/sql-map-2.dtd ">
<SqlMap namespace = "FBSubscribe">
<Delete id = "delete2">
Delete from T_UDP_SUBSCRIBE
WHERE CATEGORY_ID = # categoryId # andCONSUMER_ID = # consumerId #
</Delete>
</SqlMap>

Eighth: Jsp page writing (subscribeManage. jsp)
<Body>
<Div id = "dept_grid" class = "mini-datagrid" style = "width: 100%; height: 180px; "url =" <% = path %>/restservice/pQueryConsumers "idField =" appCode "onselectionchanged =" onSelectionChanged "selectOnLoad =" true ">
<Div property = "columns">
<Div field = "appName" width = "100" headerAlign = "center" allowSort = "true">
Customer name <input property = "editor" class = "mini-textbox" style = "width: 100%;"/>
</Div>
<Div field = "appDesc" width = "300" headerAlign = "center" allowSort = "true">
Customer description <input property = "editor" class = "mini-textbox" style = "width: 100%;"/>
</Div>
</Div>
</Div>
 
<Br/>
<Div id = "employee_grid" class = "mini-datagrid" multiSelect = "true"
Style = "width: 100%; height: 250px ;"
Url = "<% = path %>/restservice/queryByConsumerId2" idField = "guid">
<Div property = "columns">
<Div type = "checkcolumn"> </div>
<Div field = "categoryCode" width = "100" headerAlign = "center" allowSort = "true">
Data Category Code <input property = "editor" class = "mini-textbox" style = "width: 100%;"/>
</Div>
<Div field = "categoryName" width = "100" headerAlign = "center" allowSort = "true">
Data category name <input property = "editor" class = "mini-textbox" style = "width: 100%;"/>
</Div>
<Div field = "status" width = "100" headerAlign = "center" allowSort = "true">
Status <input property = "editor" class = "mini-textbox" style = "width: 100%;"/>
</Div>
</Div>
</Div>
<Br/>
<A class = "mini-button" style = "margin-left: 70%;" iconCls = "icon-edit" onclick = "updateType ('0 ') "> Start </a>
<A class = "mini-button" style = "margin-left: 2%;" iconCls = "icon-edit" onclick = "updateType ('1 ') "> stop </a>
<A class = "mini-button" style = "margin-left: 2%;" iconCls = "icon-remove" onclick = "delType"> Delete </a>


<Script type = "text/javascript">
Mini. parse ();
Var dept_grid = mini. get ("dept_grid ");
Var employee_grid = mini. get ("employee_grid ");
Dept_grid.load ();
// Linkage Query
Function onSelectionChanged (e ){
Var grid = e. sender;
Var record = grid. getSelected ();
If (record ){
Employee_grid.load ({"consumerId": record. guid });
}
}

// Delete data subscription
Function delType (){
Var row = dept_grid.getSelected ();
Var rows = employee_grid.getSelecteds ();
Var categoryId = [];
If (rows. length <1 ){
Alert ('select at least one data category! ');
Return;
} Else {
For (var I = 0; I <rows. length; I ++ ){
CategoryId. push (rows [I]. categoryCode );
}
CategoryId = categoryId. join (",");
}

If (rows. length> 0 ){
If (confirm ("are you sure you want to delete it? ")){
// Var json = {categoryId: categoryId };
// Json = mini. encode (json );
Var consumerId = row. appCode;
$. Ajax ({
Url: "<% = path %>/restservice/deleteSubscribe2 /",
Data: {data: categoryId, consumerId: consumerId, operator: operator },
Type: "post ",
Success: function (text ){
Alert (text. result );
Location. reload ();
},
Error: function (jqXHR, textStatus, errorThrown ){
Alert (jqXHR. responseText );
}
});
}

}
}
</Script>
</Bdoy>

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.