写restful接口首先要开发服务端
先定义一下请求参数对象和返回参数类型:请求参数类型(ThirdMainBillBeanReq)和响应参数类型(ThirdMainBillBeanRes)
/**
*
*/
package com.deppon.foss.module.thirdpay.shared.domain;
import java.math.BigDecimal;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author 200937
*
*/
@XmlRootElement(name = "ThirdMainBillBeanReq")
public class ThirdMainBillBeanReq {
/**
* 编号
*/
private String billNum;
/**
* 汇款时间
*/
private Date remitDate;
/**
* 汇款部门
*/
private String remitDept;
/**
* 款项所属部门
*/
private String fundDept;
/**
* 汇款金额
*/
private BigDecimal remitTance;
/**
* 汇入账号
*/
private String remitAccount;
/**
* 账号类型
*/
private String accountType;
/**
* 已使用金额
*/
private BigDecimal alreadyamount;
/**
* 汇款人
*/
private String remitPerson;
/*******************get/set************************/
public String getBillNum() {
return billNum;
}
public void setBillNum(String billNum) {
this.billNum = billNum;
}
public Date getRemitDate() {
return remitDate;
}
public void setRemitDate(Date remitDate) {
this.remitDate = remitDate;
}
public String getRemitDept() {
return remitDept;
}
public void setRemitDept(String remitDept) {
this.remitDept = remitDept;
}
public String getFundDept() {
return fundDept;
}
public void setFundDept(String fundDept) {
this.fundDept = fundDept;
}
public BigDecimal getRemitTance() {
return remitTance;
}
public void setRemitTance(BigDecimal remitTance) {
this.remitTance = remitTance;
}
public String getRemitAccount() {
return remitAccount;
}
public void setRemitAccount(String remitAccount) {
this.remitAccount = remitAccount;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public BigDecimal getAlreadyamount() {
return alreadyamount;
}
public void setAlreadyamount(BigDecimal alreadyamount) {
this.alreadyamount = alreadyamount;
}
public String getRemitPerson() {
return remitPerson;
}
public void setRemitPerson(String remitPerson) {
this.remitPerson = remitPerson;
}
}
/**
*
*/
package com.deppon.foss.module.thirdpay.shared.domain;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author 200937
*
*/
@XmlRootElement(name = "ThirdMainBillBeanRes")
public class ThirdMainBillBeanRes {
/**
* 是否成功(1、成功,0、失败)
*/
private String isSuccess;
/**
* 失败原因
*/
private String faileReason;
/**
* 编号
*/
private String billNum;
/***************get/set*******************/
public String getIsSuccess() {
return isSuccess;
}
public void setIsSuccess(String isSuccess) {
this.isSuccess = isSuccess;
}
public String getFaileReason() {
return faileReason;
}
public void setFaileReason(String faileReason) {
this.faileReason = faileReason;
}
public String getBillNum() {
return billNum;
}
public void setBillNum(String billNum) {
this.billNum = billNum;
}
}
然后写一个service进行业务处理:
/**
*
*/
package com.deppon.foss.module.thirdpay.server.service;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import com.deppon.foss.module.thirdpay.shared.domain.ThirdMainBillBeanReq;
import com.deppon.foss.module.thirdpay.shared.domain.ThirdMainBillBeanRes;
/**
* @author 200937
*
*/
//@Produces({"application/json"})
//@Consumes({"application/json"})
@Produces({/*MediaType.APPLICATION_XML,*/MediaType.APPLICATION_JSON })
@Consumes({MediaType.APPLICATION_JSON/*,MediaType.APPLICATION_XML*/})
public interface IThirdMainBillService {
/**
* APP对接财务自助-第三方汇款-即时到账(保存信息)
* @author 200937
* @date 2014-11-05
* @param ThirdMainBillBeanReq
* @return ThirdMainBillBeanRes
*/
@POST
@Path("/InsertThirdMainBill")
public @ResponseBody ThirdMainBillBeanRes InsertThirdMainBill (@RequestBody ThirdMainBillBeanReq thirdMainBillReq);
}
/**
*
*/
package com.deppon.foss.module.thirdpay.server.service.impl;
import java.math.BigDecimal;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import com.deppon.fins.module.otherReceive.shared.domain.ESBHeaderConstant;
import com.deppon.foss.module.commonUtils.server.service.IFinsLogService;
import com.deppon.foss.module.thirdpay.server.dao.IThirdMainBillDao;
import com.deppon.foss.module.thirdpay.server.service.IThirdMainBillService;
import com.deppon.foss.module.thirdpay.shared.domain.ThirdMainBillBean;
import com.deppon.foss.module.thirdpay.shared.domain.ThirdMainBillBeanReq;
import com.deppon.foss.module.thirdpay.shared.domain.ThirdMainBillBeanRes;
/**
* @author 200937
*
*/
public class ThirdMainBillServiceImpl implements IThirdMainBillService {
@Resource
private IThirdMainBillDao thirdMainBillDao;
@Resource
private IFinsLogService finsLogService;
@Context HttpServletRequest request;
@Context HttpServletResponse response;
/**
* APP对接财务自助-第三方汇款-即时到账(保存信息)
* @author 200937
* @date 2014-11-05
* @param ThirdMainBillBeanReq
* @return ThirdMainBillBeanRes
*/
@Override
public @ResponseBody ThirdMainBillBeanRes InsertThirdMainBill (@RequestBody ThirdMainBillBeanReq thirdMainBillReq) {
//设置相应header
response.setHeader(ESBHeaderConstant.VERSION, request.getHeader(ESBHeaderConstant.VERSION));
response.setHeader(ESBHeaderConstant.ESBSERVICECODE, request.getHeader(ESBHeaderConstant.ESBSERVICECODE));
response.setHeader(ESBHeaderConstant.REQUESTID, request.getHeader(ESBHeaderConstant.REQUESTID));
response.setHeader(ESBHeaderConstant.BUSINESSID, request.getHeader(ESBHeaderConstant.BUSINESSID));
response.setHeader(ESBHeaderConstant.MESSAGEFORMAT, request.getHeader(ESBHeaderConstant.MESSAGEFORMAT));
response.setHeader(ESBHeaderConstant.EXCHANGEPATTERN, request.getHeader(ESBHeaderConstant.EXCHANGEPATTERN));
response.setHeader(ESBHeaderConstant.SOURCESYSTEM, request.getHeader(ESBHeaderConstant.SOURCESYSTEM));
response.setHeader(ESBHeaderConstant.BACKSERVICECODE, request.getHeader(ESBHeaderConstant.BACKSERVICECODE));
response.setHeader(ESBHeaderConstant.TARGETSYSTEM, "FINS");
response.setHeader(ESBHeaderConstant.RESPONSEID, UUID.randomUUID().toString());
ThirdMainBillBeanRes thirdMainBillRes=new ThirdMainBillBeanRes();
ThirdMainBillBean thirdMainBill =new ThirdMainBillBean();
//判断是否为空并且将请求对象转换为第三方汇款对象
if(thirdMainBillReq == null){
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("请求对象为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getBillNum() !=null){
thirdMainBill.setBillNum(thirdMainBillReq.getBillNum());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("编号为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getRemitDate() !=null){
thirdMainBill.setRemitDate(thirdMainBillReq.getRemitDate());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("汇款时间为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getRemitDept()!=null){
thirdMainBill.setRemitDept(thirdMainBillReq.getRemitDept());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("汇款部门为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getFundDept()!=null){
thirdMainBill.setFundDept(thirdMainBillReq.getFundDept());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("款项所属部门为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getRemitTance()!=null){
thirdMainBill.setRemitTance(thirdMainBillReq.getRemitTance());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("汇款金额为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getRemitAccount()!=null){
thirdMainBill.setRemitAccount(thirdMainBillReq.getRemitAccount());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("汇入账号为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getAccountType()!=null){
thirdMainBill.setAccountType(thirdMainBillReq.getAccountType());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("账号类型为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getAlreadyamount()!=null){
thirdMainBill.setAlreadyamount(thirdMainBillReq.getAlreadyamount());
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("已使用金额为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
if(thirdMainBillReq.getRemitPerson()!=null){
thirdMainBill.setRemitPerson(thirdMainBillReq.getRemitPerson());
}/*else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("汇款人为空");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}*/
//将初始化的初始化
//未使用金额
BigDecimal unuseamount=thirdMainBill.getRemitTance().subtract(thirdMainBill.getAlreadyamount());
thirdMainBill.setUnuseamount(unuseamount);
boolean result=false;
try{
result=thirdMainBillDao.addThirdMainBill(thirdMainBill);
if(result){
thirdMainBillRes.setIsSuccess("1");
thirdMainBillRes.setBillNum(thirdMainBill.getBillNum());
response.setHeader(ESBHeaderConstant.RESULTCODE, "1");
return thirdMainBillRes;
}else{
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("插入失败");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
return thirdMainBillRes;
}
}catch (Exception e) {
// TODO: handle exception
thirdMainBillRes.setIsSuccess("0");
thirdMainBillRes.setFaileReason("插入数据错误");
response.setHeader(ESBHeaderConstant.RESULTCODE, "0");
finsLogService.debug(ThirdMainBillServiceImpl.class, "InsertThirdMainBill", "", "插入数据错误", e);
return thirdMainBillRes;
}
}
}
具体的DAO层就不写了。
接着就是配置Spring配置问价了
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
记住这里面加了一个json转换器jacksonJsonProvider类,如何加上这个使用的访问方式就应该用字节流,参数为json String形式的才可以。
我有一个APPClient客户端访问的这个客户端访问就要服务端去掉JacksonJsonProvider这个json转化器了。
/**
*
*/
package client;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import domain.ESBHeaderConstant;
import domain.OtherCheckSheetReq;
import domain.OtherCheckSheetRes;
import domain.OtherCheckSheetletn;
import domain.ThirdMainBillBeanReq;
import domain.ThirdMainBillBeanRes;
import domain.WorkFlowInfo;
import domain.WorkFlowReq;
import domain.WorkFlowRes;
import javax.ws.rs.core.MultivaluedMap;
/**
* @author 200937
*
*/
public class AppWebClient {
private static final String CATEGORY_URL =
"http://localhost:8080/financManager/webservice/ThirdMainBillService/";
// "http://10.224.72.116:8881/wfs/webservice/verifyWorkFlow/";
// "http://192.168.67.12:8580/esb2/rs/";
private static final String TYPE_JSON = "application/json";
public static void main(String[] args) {
// TODO Auto-generated method stub
WebClient client = WebClient.create(CATEGORY_URL);
client.path("InsertThirdMainBill").accept(TYPE_JSON).type(TYPE_JSON);//"InsertThirdMainBill".encoding("UTF-8")
// System.out.println("begin");
// WorkFlowReq workFlowReq=new WorkFlowReq();
// WorkFlowInfo workFlowInfo=new WorkFlowInfo();
// workFlowInfo.setProcessInstId("2222");
// workFlowInfo.setProcessDefName("asdgadg");
//
// WorkFlowRes workFlowRes=client.post(workFlowReq, WorkFlowRes.class);
// System.out.println(workFlowRes.toString());
//
client.header(ESBHeaderConstant.VERSION, "0.1");
client.header(ESBHeaderConstant.ESBSERVICECODE, "ESB_APP2ESB_INSERT_FINS");
client.header(ESBHeaderConstant.REQUESTID, UUID.randomUUID().toString());
client.header(ESBHeaderConstant.BUSINESSID, UUID.randomUUID().toString());
client.header(ESBHeaderConstant.MESSAGEFORMAT, "json");
client.header(ESBHeaderConstant.EXCHANGEPATTERN, "1");
client.header(ESBHeaderConstant.SOURCESYSTEM, "APP");
client.header("accept", "application/json");//text/html,application/xml,
client.header("Content-Type", "application/json;charset=UTF-8"); //;charset=UTF-8
ThirdMainBillBeanReq thirdMainBillBeanReq =new ThirdMainBillBeanReq();
thirdMainBillBeanReq.setBillNum("w0112234443321");//编号
Date remitDate=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
remitDate=sdf.parse("1991-05-19");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thirdMainBillBeanReq.setRemitDate(remitDate);//汇款时间
thirdMainBillBeanReq.setRemitDept("DP10270");//汇款部门
thirdMainBillBeanReq.setFundDept("DP24111");//款项所属部门
BigDecimal remitTance=new BigDecimal(145);
thirdMainBillBeanReq.setRemitTance(remitTance);//汇款金额
thirdMainBillBeanReq.setRemitAccount("27105555");//汇入账号
thirdMainBillBeanReq.setAccountType("支付宝");//账号类型
BigDecimal alreadyamount=new BigDecimal(55);
thirdMainBillBeanReq.setAlreadyamount(alreadyamount);//已使用金额
thirdMainBillBeanReq.setRemitPerson("李伟强");
ThirdMainBillBeanRes s=client.post(thirdMainBillBeanReq,ThirdMainBillBeanRes.class);
System.out.println(s.toString());
}
}
这个是定义client.header("Content-Type", "application/json;charset=UTF-8"); //;charset=UTF-8
是将传递的参数对象转换整json方式这个是jar包中自动转换的。
还有一种是先将对象转换成Json String然后通过字节流将对象传递过去然后在Spring配置文件中加入Json转换器(Json转换器请看上一篇json转化器)的形式。
我做了一个客户端但不是访问这个的 你们改访问对象即可。
/**
*
*/
package client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import common.JsonUtil;
import domain.Accessory;
import domain.ReceivableInfo;
import domain.SpareAreaSubReq;
import domain.SpareAreaSubletn;
/**
* @author 200937
*
*/
public class ClentTest {
public static void main(String [] arg) throws Exception{
URL url = new URL("http://localhost:8080/financManager/webservice/SpareAreaSubRestServer/addSpareAreaSubSP");// 提交地址
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);// 打开写入属性
httpURLConnection.setDoInput(true);// 打开读取属性
httpURLConnection.setRequestMethod("POST");// 设置提交方法
httpURLConnection.setRequestProperty("Content-Type","Application/json;charset=UTF-8");//;charset=UTF-8
// httpURLConnection.setRequestProperty("SOAPAction", " http://WebXml.com.cn/getWeatherbyCityName");
// httpURLConnection.setRequestProperty("User-Agent", "Jakarta Commons-HttpClient/3.1");
httpURLConnection.setConnectTimeout(50000);// 连接超时时间
httpURLConnection.setReadTimeout(50000);
httpURLConnection.connect();
// SpareAreaSubReq spareAreaSubReq =new SpareAreaSubReq();
// SpareAreaSubletn spareAreaSub=new SpareAreaSubletn();
// spareAreaSub.setSubletnumber("YQ2014110001");
// spareAreaSubReq.setSpareAreaSubletn(spareAreaSub);
SpareAreaSubReq spareAreaSubReq =new SpareAreaSubReq();
SpareAreaSubletn spareAreaSub=new SpareAreaSubletn();
spareAreaSub.setSubletnumber("YQ2014110001");
ReceivableInfo receivableInfo =new ReceivableInfo();
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
String str="2014-10-12";
Date date=null;
try {
date = sim.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
receivableInfo.setReceivabledate(date);//应收日期
receivableInfo.setReceivablesubletsum(111.00);//应收金额
ReceivableInfo receivableInfo2 =new ReceivableInfo();
String str2="2014-10-12";
Date date2=null;
try {
date2 = sim.parse(str2);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// receivableInfo2.setReceivabledate(date2);//应收日期
// receivableInfo2.setReceivablesubletsum(111.00);//应收金额
List
receivableInfoList.add(receivableInfo);
receivableInfoList.add(receivableInfo2);
spareAreaSub.setReceivableInfo(receivableInfoList);//应收信息
spareAreaSub.setContratworknum("2000033");//合同工作流号
spareAreaSub.setSubletdeptcode("DP10270");//转租部门编码
spareAreaSub.setFinancedeptcode("DP10244");//财务部编码
spareAreaSub.setSignType("OVER");//签订类型NEW_SIGN\RENEW\ChANGE\OVER
spareAreaSub.setPaymenttype(12);//付款方式
spareAreaSub.setLeaseename("卡卡西");//承租方名称
String str3="2014-10-12";
Date date3=null;
try {
date3 = sim.parse(str3);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spareAreaSub.setSubletstartdate(date3);//转租起租日期
String str4="2014-10-12";
Date date4=null;
try {
date4 = sim.parse(str4);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spareAreaSub.setSubletenddate(date4);//转租结束日期
spareAreaSub.setDepositaccount(200);//押金金额
String str5="2014-10-12";
Date date5=null;
try {
date5 = sim.parse(str5);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spareAreaSub.setRentfreestartdate(date5);//免租开始日期
String str6="2014-10-12";
Date date6=null;
try {
date6 = sim.parse(str6);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spareAreaSub.setRentfreeenddate(date6);//免租结束日期
String str7="2014-10-10";
Date date7=null;
try {
date7 = sim.parse(str7);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
spareAreaSub.setAdvanceenddate(date7);
Accessory acce=new Accessory();
acce.setAccessoryName("生活真美好");
acce.setAccessoryType("Excel");
acce.setAccessoryUrl("http//..");
List
acceList.add(acce);
spareAreaSub.setAccessory(acceList);
spareAreaSubReq.setSpareAreaSubletn(spareAreaSub);
String JsonSpareAreaSubReq=JsonUtil.getJSONString(spareAreaSubReq);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream()));
out.write(JsonSpareAreaSubReq);
out.flush();
out.close();
//读取post之后的返回值
BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) httpURLConnection.getInputStream(),"UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
System.out.println("client:" + sb.toString());
httpURLConnection.disconnect();//断开连接
}
}