1. 根据 客户的需求,要从oa系统发起建单操作,那么先就要从客户系统的oa系统页面,发起一个获取我们系统token的接口,然后oa那边再将token放到请求头里,把json报文放在body体里,我们指定一个接口接收。完整代码如下:
package com.hand.hcf.app.expense.oareport.web;
import com.hand.hcf.app.expense.client.extraApi.CompanyLevelInterface;
import com.hand.hcf.app.expense.oareport.client.OrganizationForOAFeignClient;
import com.hand.hcf.app.expense.oareport.domain.ExpenseReportOaLog;
import com.hand.hcf.app.expense.oareport.domain.OaFormRespMessage;
import com.hand.hcf.app.expense.oareport.dto.*;
import com.hand.hcf.app.expense.oareport.service.*;
import com.hand.hcf.app.expense.report.domain.ExpenseReportHeader;
import com.hand.hcf.app.expense.oareport.service.ExpenseOAReportLineImportService;
import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.ZonedDateTime;
@RestController
@RequestMapping("/api/expense/oareport")
public class ExpenseOAReportController {
@Autowired
ExpenseOAReportHeaderService expenseOAReportHeaderService;
@Autowired
OrganizationForOAFeignClient organizationForOAFeignClient;
@Autowired
ExpenseOAReportLineImportService expenseOAReportLineImportService;
@Autowired
ExpenseReportOaLogService expenseReportOaLogService;
@PostMapping("/save")
@ApiOperation(value = "OA表单数据", notes = "OA表单数据 ")
public ResponseEntity<OaFormRespMessage> saveExpenseOAReport(@ApiParam(value = "OA表单数据") @RequestBody @Valid ExpenseOAReportHeaderDTO dto,@RequestHeader("Authorization") String token){
ExpenseReportOaLog expenseReportOaLog = new ExpenseReportOaLog();
//插入日志表
JSONObject send_json = JSONObject.fromObject(dto);
expenseReportOaLog.setCreatedDate(ZonedDateTime.now());
OaFormRespMessage oaFormRespMessage = expenseOAReportHeaderService.saveOAExpenseReport(dto,token);
JSONObject return_json = JSONObject.fromObject(oaFormRespMessage);
expenseReportOaLog.setTaskId(dto.getTaskId());
if (send_json.toString().length()>4000){
expenseReportOaLog.setSendMsg(send_json.toString().substring(0,4000));
}else{
expenseReportOaLog.setSendMsg(send_json.toString());
}
expenseReportOaLog.setReturnMsg(return_json.toString());
expenseReportOaLog.setLastUpdatedDate(ZonedDateTime.now());
expenseReportOaLogService.insertOrUpdate(expenseReportOaLog);
return ResponseEntity.ok(oaFormRespMessage);
}
@PostMapping("/next/save")
@ApiOperation(value = "OA表单下一步", notes = "根据OA传过来的数据自动生成报销单")
public ResponseEntity<ExpenseReportHeader> saveExpenseReportInfo(@ApiParam(value = "报账单头") @RequestBody @Valid ExpenseReportHeader expenseReportHeader,
@ApiParam(value = "OA报账头id") @RequestParam Long headerId){
return ResponseEntity.ok(expenseOAReportHeaderService.saveExpenseOAReportInfo(expenseReportHeader,headerId));
}
@PostMapping("/get/oatoken")
@ApiOperation(value = "OA获取token", notes = "OA获取token")
public String getOaToken(@ApiParam(value = "员工工号") @RequestParam String employeeCode, @ApiParam(value = "账套id") @RequestParam Long setOfBooksId){
return expenseOAReportHeaderService.getOaToken(employeeCode,setOfBooksId);
}
}
发送http请求:
package com.hand.hcf.app.expense.oareport.client;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class OaToken {
public String doPostData(String url, String json) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
String result = "";
HttpResponse res = null;
try {
StringEntity s = new StringEntity(json.toString(), "UTF-8");
s.setContentType("application/json");
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json; charset=utf-8");
post.setEntity(s);
res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(res.getEntity());
return result;
}
} catch (Exception e) {
if(res == null) {
return "HttpResponse 为 null!";
}
throw new RuntimeException(e);
}
if(res == null || res.getStatusLine() == null) {
return "无响应";
}
return result;
}
}