servlet接口,报文格式
程序员文章站
2022-07-10 15:25:56
...
使用servlet的方式作为接口服务端,以报文的格式传递数据。
【1】首先写一个servlet
package ***;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class InvoiceProgressServer extends HttpServlet {
@Autowired(required=false)
@Qualifier("queryProgressServiceImpl")
private QueryProgressService queryProgressService;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应编码
response.setCharacterEncoding("GBK");
PrintWriter pw = null;
ServletInputStream is = null;
String requestXmlStr = "";
try {
pw = response.getWriter();
is = request.getInputStream();
requestXmlStr = IOUtils.toString(is, "UTF-8");
queryProgressService = (QueryProgressService) SpringContextHolder.getBean("queryProgressServiceImpl");
Map params = queryProgressService.valiXmlStr(requestXmlStr);
String responseXmlStr = new String(params.get("backXml").toString().getBytes("GBK"), "GBK");
pw.write(responseXmlStr);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(pw != null){
pw.close();
}
if(is != null){
is.close();
}
}
}
}
在web.xml里面进行配置:
<servlet>
<servlet-name>invoiceProgressServer</servlet-name>
<servlet-class>com.tt.InvoiceProgressServer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>invoiceProgressServer</servlet-name>
<url-pattern>/invoiceProgressServer</url-pattern>
</servlet-mapping>
【2】接下来写service,主要对于接受的报文数据进行校验,并调用dao然后进行数据查询。
package *;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jsoup.select.Evaluator.IsEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service("queryProgressServiceImpl")
public class QueryProgressServiceImpl extends BasicService implements QueryProgressService {
@Autowired(required=false)
@Qualifier("queryProgressDaoImpl")
private QueryProgressDao queryProgressDao;
/**
* 验证报文是否合法
* @param XmlStr
* @return
*/
@Override
public Map valiXmlStr(String XmlStr) {
Map map = new HashMap();
String systemcode = "";
String reqtime = "";
String jkjjlx = "";
String pch = "";
String zbs = "";
String cxfs = "";
StringBuffer sbf = new StringBuffer();
sbf.append("<?xml version=\"1.0\" encoding=\"GBK\"?><PACKET><HEAD>");
try {
//系统来源
systemcode = XmlUtil.getIntervalValue(XmlStr, "<SYSTEMCODE>", "</SYSTEMCODE>");
int num = XmlUtil.NumberOfStatisticalStrings(XmlStr, "<REQINFO>");
//验证系统来源
if("".equals(systemcode)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>" +
"<ERRORMSG>系统来源不能为空</ERRORMSG>" +
"</BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}
//请求时间
reqtime = XmlUtil.getIntervalValue(XmlStr, "<REQTIME>", "</REQTIME>");
if("".equals(reqtime)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>" +
"<ERRORMSG>请求时间不能为空</ERRORMSG>" +
"</BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}
if(!DateUtil.isFormatDate(reqtime,"yyyy-MM-dd HH:mm:ss")){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>" +
"<ERRORMSG>请求时间格式错误</ERRORMSG>" +
"</BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}
//接口交易类型
jkjjlx = XmlUtil.getIntervalValue(XmlStr, "<JKJYLX>", "</JKJYLX>");
if(!"123456".equals(jkjjlx)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>");
if("".equals(jkjjlx)){
sbf.append("<ERRORMSG>接口交易类型不能为空</ERRORMSG></BASIC></BODY></PACKET>");
}else{
sbf.append("<ERRORMSG>接口交易类型错误</ERRORMSG></BASIC></BODY></PACKET>");
}
map.put("backXml", sbf);
return map;
}
pch = XmlUtil.getIntervalValue(XmlStr, "<PCH>", "</PCH>");
if("".equals(pch)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>" +
"<ERRORMSG>批次号不能为空</ERRORMSG>" +
"</BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}
zbs = XmlUtil.getIntervalValue(XmlStr, "<ZBS>", "</ZBS>");
if("".equals(zbs)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>");
sbf.append("<ERRORMSG>总笔数不能为空</ERRORMSG></BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}else if(!zbs.equals(num+"")){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>");
sbf.append("<ERRORMSG>总笔数与实际笔数不符</ERRORMSG></BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}
cxfs = XmlUtil.getIntervalValue(XmlStr, "<CXFS>", "</CXFS>");
if("".equals(cxfs)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>");
sbf.append("<ERRORMSG>查询方式不能为空</ERRORMSG></BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;
}else if(!"1".equals(cxfs)&&"2".equals(cxfs)&&"3".equals(cxfs)&&"4".equals(cxfs)){
sbf.append("<JSZTBS>400</JSZTBS>");
sbf.append("<JSCGBS>0</JSCGBS>");
sbf.append("<JSSBBS>0</JSSBBS></HEAD>");
sbf.append("<BODY><BASIC><SBFPQQLSH></SBFPQQLSH>" +
"<ERRORCODE>9999</ERRORCODE>");
sbf.append("<ERRORMSG>查询方式输入错误</ERRORMSG></BASIC></BODY></PACKET>");
map.put("backXml", sbf);
return map;</span><pre name="code" class="java"><span style="font-size: 18px;">}
之后大量的业务逻辑给省略了································
接收的报文数据校验之后,再将查询所需的字段从报文中解析出来。
【3】dao层大部分是sql语句,代码不贴了。
【4】测试类test:
package **;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class TestInvoiceProgress {
public static void main(String[] args) {
TestInvoiceProgress t = new TestInvoiceProgress();
String ip = "http://127.0.0.1:8888/my/invoiceProgressServer";
HttpClient client = new HttpClient();
String xml = t.buildXml();
StringBuffer sbf = new StringBuffer();
BufferedReader br = null;
InputStream stream = null;
try {
PostMethod method = new PostMethod(ip);
method.setRequestHeader("Content-Type",
"application/xml;charset=GBK");
method.setRequestBody(xml);
client.executeMethod(method);
String requestEncoding = EncodingTest.getEncoding(xml);
System.out.println("请求的编码为 :"+requestEncoding);
// 打印返回的信息
stream = (InputStream) method.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(
stream, "GBK"));
String line;
while(null!= (line = br.readLine())){
sbf.append(line).append("\n");
}
method.releaseConnection();
//String responseXml = new String(sbf.toString().getBytes("GBK"),"UTF-8");
//String sbfs = new String(sbf.toString().getBytes("GBK"),"GBK");
System.out.println("返回来的编码为 :"+EncodingTest.getEncoding(sbf.toString()));
System.out.println(sbf.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
if(stream != null){
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String buildXml(){
StringBuilder xml = new StringBuilder();
xml.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
xml.append("<PACKET>");
xml.append("<HEAD><SYSTEMCODE>12321</SYSTEMCODE>" +
"<REQTIME>2017-06-14 12:22:33</REQTIME><JKJYLX>0000</JKJYLX><PCH>22</PCH>" +
"<ZBS>10</ZBS><CXFS>4</CXFS></HEAD>");
xml.append("<BODY><REQINFOS><REQINFO><FPQQLSH>123124123</FPQQLSH><FPDM>124124125</FPDM><FPHM>123124</FPHM>" +
"<KPRQ>2017-06-20 11:31:15</KPRQ><JSHJ>1.28</JSHJ></REQINFO>");
xml.append("</REQINFOS></BODY></PACKET>");
System.out.println(xml);
return xml.toString();
}
}
【5】请求报文数据格式示例
<?xml version="1.0" encoding="GBK"?>
<PACKET>
<HEAD>
<SYSTEMCODE></SYSTEMCODE>
<REQTIME></REQTIME>
<JKJYLX></JKJYLX>
<PCH></PCH>
<ZBS>1</ZBS>
<CXFS>2</CXFS>//查询方式
</HEAD>
<BODY>
<REQINFOS count=>
····
</REQINFOS>
</BODY>
</PACKET>
【6】返回报文数据格式示例
<?xml version="1.0" encoding="GBK"?>
<PACKET>
<HEAD>
<JSZTBS>接收状态标识</JSZTBS>
<JSCGBS>接收成功笔数</JSCGBS>
<JSSBBS>接收失败笔数</JSSBBS>
</HEAD>
<BODY>
<BASICS>
<BASIC>
<SBFPQQLSH></SBFPQQLSH>
<ERRORCODE></ERRORCODE>
<ERRORMSG></ERRORMSG>
</BASIC>
<BASIC>...</BASIC>
</BASICS>
<RESPINFOS count="1">
<RESPINFO>
....
</RESPINFO>
</RESPINFOS>
</BODY>
</PACKET>
总结:之前一直碰到了接口响应报文中文乱码的问题。寻求了很多解决办法,都没解决,后来发现只要在servlet里面加上一句代码就OK了。设置他的响应编码就行了。
第一次做一个接口服务,之前查资料发现也可以用webService的方式,但是那是没怎么使用过的方式,便还是采用了自己熟悉的servlet的方式。而且使用报文的方式,数据清晰明白,也是第一次接触使用。
下一篇: 报文格式:xml 、定长报文、变长报文