第一步:将jar包导入项目中,jar包包括
- fastjson-1.2.47.jar
- gson-2.8.5.jar
- jackson-core-asl-1.6.3.jar
- jackson-jaxrs-1.6.3.jar
- jackson-mapper-asl-1.6.3.jar
- jaxrs-api-2.2.3.GA.jar
- jettison-1.1.jar
- resteasy-jackson-provider-2.2.3.GA.jar
- resteasy-jaxrs-2.2.3.GA.jar
- resteasy-spring-2.2.3.GA.jar
- scannotation-1.0.3.jar
- webservice_call_bdas.jar
- xmlbeans-2.6.0.jar
第二步:在web.xml中增加导入 resteasy的配置,可以采用不自动扫描,注释掉resteasy.scan和ResteasyBootstrap部分
<!--
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
-->
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.shinow.common.rest.StatusService</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/</param-value>
</context-param>
<!-- <listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
-->
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/donate/*</url-pattern>
</servlet-mapping>
第三步:在web.xml里的strust2配置中增加,/donate/*不在过滤器内
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>struts.action.excludePattern</param-name>
<param-value>/servlet/FileUploadServlet.servlet,/editor/HtmlEdiortFileManager.servlet*,/servlet/ValidCodeServlet,/shinow_service/*,/donate/*</param-value>
</init-param>
</filter>
第四步:在applicationContext_webservice.xml里增加如下配置
<bean name="statusService" class="com.shinow.common.rest.StatusService"><property name="usersService" ref="usersService"></property> </bean><bean name="usersService" class="com.shinow.bfrs.service.register.TRecBldUsersServiceImpl"></bean>
第五步:在shinow_security.properties里修改如下增加|(/donate.*)
sercurity.nofilter.pattern=(/tmp/.*)|(/common/excute\\.action)|(/login\\.action)|(/css/.*)|(/skin/.*)|(/javascript/.*)|(/shinow_service/upload.*)|(/favicon.ico)|(/servlets/image/.*)|(/shinow_service/BloodReturnDownload.*)|(/donate.*)
第六步:新建StatusService,类前增加注解@Path("/donate")
package com.shinow.common.rest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shinow.bfrs.service.register.ITRecBldUsersService;
import com.shinow.bfrs.service.register.TRecBldUsersServiceImpl;
@Path("/donate")
public class StatusService {
protected final Log logger = LogFactory.getLog(StatusService.class);
private ITRecBldUsersService usersService;
/**
* 探针
* @param servletContext
* @return
*/
@GET
@Path("/status")
@Produces("application/json;charset=UTF-8")
public Map<String,Object> status(@Context ServletContext servletContext) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("status", "ok");
return map;
}
@POST
@Path("/expense")
@Produces("text/plain;charset=UTF-8")
public String getexpense(@RequestBody String paramStr,@Context ServletContext servletContext){
String returnStr="";
Map<String,Object> resultMap = new HashMap<String,Object>();
String paramStrAfterDecrypt = DesUtils.decrypt(paramStr);
ClientParam clientParam = new Gson().fromJson(paramStrAfterDecrypt, new TypeToken<ClientParam>(){}.getType());
try{
if(null==clientParam.getReqType()||clientParam.getReqType().equals("")){
resultMap.put("IsSuccess", "false");
resultMap.put("ErrorMessage", "请求类型不能为空(1:退费码查询 2:证件码查询)");
resultMap.put("Data","null");
String resultMapByGsonStr =GsonUtil.toJson(resultMap);
System.out.println(resultMapByGsonStr);
returnStr = DesUtils.encrypt(resultMapByGsonStr);
return returnStr;
}
/**
if(null==token||token.equals("")){
resultMap.put("IsSuccess", "false");
resultMap.put("ErrorMessage", "token不能为空");
resultMap.put("Data",null);
String resultMapByGsonStr = GsonUtil.toJson(resultMap);
returnStr = DesUtils.encrypt(resultMapByGsonStr);
return returnStr;
}**/
if(clientParam.getReqType().equals("1")){
if(null==clientParam.getBillCode()||clientParam.getBillCode().equals("")){
resultMap.put("IsSuccess", "false");
resultMap.put("ErrorMessage", "当用请求类型为1退费码查询时,退费编号不能为空");
resultMap.put("Data","null");
String resultMapByGsonStr = GsonUtil.toJson(resultMap);
System.out.println(resultMapByGsonStr);
returnStr = DesUtils.encrypt(resultMapByGsonStr);
return returnStr;
}
}else if(clientParam.getReqType().equals("2")){
if(null==clientParam.getIdentityCode()||clientParam.getIdentityCode().equals("")){
resultMap.put("IsSuccess", "false");
resultMap.put("ErrorMessage", "当用请求类型为2证件号码查询时,证件号码不能为空");
resultMap.put("Data","null");
String resultMapByGsonStr = GsonUtil.toJson(resultMap);
System.out.println(resultMapByGsonStr);
returnStr = DesUtils.encrypt(resultMapByGsonStr);
return returnStr;
}
}
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
TRecBldUsersServiceImpl usersService = (TRecBldUsersServiceImpl)ctx.getBean("usersService");
List<DataResult> listData = usersService.queryExpenseInfo(clientParam);
if(null!=listData&&listData.size()>0 ){
DataResult dataResult = listData.get(0);
String dataResultJsonStr = GsonUtil.toJsonObj(dataResult);
resultMap.put("IsSuccess", true);
resultMap.put("ErrorMessage", "null");
resultMap.put("Data", dataResultJsonStr);
String resultMapByGsonStr = GsonUtil.toJson(resultMap);
System.out.println(resultMapByGsonStr);
returnStr = DesUtils.encrypt(resultMapByGsonStr);
return returnStr;
}
}catch(Exception e){
e.printStackTrace();
resultMap.put("IsSuccess", "false");
resultMap.put("ErrorMessage", e.getMessage());
resultMap.put("Data","null");
String resultMapByGsonStr = GsonUtil.toJson(resultMap);
System.out.println(resultMapByGsonStr);
returnStr = DesUtils.encrypt(resultMapByGsonStr);
return returnStr;
}
return returnStr;
}
public ITRecBldUsersService getUsersService() {
return usersService;
}
public void setUsersService(ITRecBldUsersService usersService) {
this.usersService = usersService;
}
public static void main(String args[]){
ClientParam clientParam = new ClientParam();
clientParam.setReqType("2");
clientParam.setBillCode("");
clientParam.setIdentityCode("510403194603251757");
String clientParamStr = GsonUtil.toJson(clientParam);
String paramStr = DesUtils.encrypt(clientParamStr);
System.out.println(paramStr);
}
}
第七步 新增DataResult.java
package com.shinow.common.rest;
import java.math.BigDecimal;
import java.util.Date;
import com.google.gson.annotations.Expose;
public class DataResult{
@Expose
private String billCode;
@Expose
private String patient;
@Expose
private String identityType;
@Expose
private String identityCode;
@Expose
private BigDecimal money;
@Expose
private Date payTime;
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getPatient() {
return patient;
}
public void setPatient(String patient) {
this.patient = patient;
}
public String getIdentityType() {
return identityType;
}
public void setIdentityType(String identityType) {
this.identityType = identityType;
}
public String getIdentityCode() {
return identityCode;
}
public void setIdentityCode(String identityCode) {
this.identityCode = identityCode;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
public Date getPayTime() {
return payTime;
}
public void setPayTime(Date payTime) {
this.payTime = payTime;
}
}
第八步,新增ClientParam.java
package com.shinow.common.rest;
public class ClientParam {
private String reqType;
private String billCode;
private String identityCode;
public String getReqType() {
return reqType;
}
public void setReqType(String reqType) {
this.reqType = reqType;
}
public String getBillCode() {
return billCode;
}
public void setBillCode(String billCode) {
this.billCode = billCode;
}
public String getIdentityCode() {
return identityCode;
}
public void setIdentityCode(String identityCode) {
this.identityCode = identityCode;
}
}
第九步,新增DesUtils.java
package com.shinow.common.rest;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DesUtils {
private static final String PASSWORD = "shinow90";
public DesUtils() {
}
/**
* 加密
* @param datasource
* @return
*/
public static String encrypt(String datasource) {
try{
DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
//创建一个密匙工厂,获取secretKey
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKey);
//指定获取DES的Cipher对象
Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
//数据加密
return parseByte2HexStr(cipher.doFinal(datasource.getBytes("utf-8")));
}catch(Throwable e){
e.printStackTrace();
}
return null;
}
/**
* 解密
* @param src
* @return
*/
public static String decrypt(String src){
try{
// 创建一个DESKeySpec对象,PASSWORD可任意指定
DESKeySpec desKey = new DESKeySpec(PASSWORD.getBytes());
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 生成密钥
SecretKey secretkey = keyFactory.generateSecret(desKey);
// 指定获取DES的Cipher对象
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, secretkey, new SecureRandom());
// 真正开始解密操作
return new String(cipher.doFinal(parseHexStr2Byte(src)));
}catch(Throwable e){
e.printStackTrace();
}
return null;
}
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
第十步,新增GsonUtil.java,
package com.shinow.common.rest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.HashMap;
public class GsonUtil {
private GsonUtil() {
}
private static Gson gson = new Gson();
public static Gson getInstance() {
return gson;
}
public static String toJsonObj(Object obj) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
return gson.toJson(obj);
}
public static String toJson(Object obj) {
return gson.toJson(obj);
}
public static <T> T fromJson(String json, Type type) {
try {
return gson.fromJson(json, type);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static HashMap<String, JsonObject> toGsonMap(String jsonStr) {
HashMap<String, JsonObject> res = null;
try {
res = gson.fromJson(jsonStr, new TypeToken<HashMap<String, JsonObject>>() {
}.getType());
} catch (Exception e) {
}
return res;
}
}