目录
三、ServiceImpl层方法调用(SpringBoot框架,大家能看懂就好)
背景:做一个项目的接口对接,用Getman\postman等接口调试工具都是可以的。用程序运行就报415.
发现我再调用甲方API接口的时候,有一个奇怪的问题,就是我写代码调用甲方API接口时返回HTTP状态码是415,返回错误:The requested media type is not supported. Supported media types are "application/json"很奇怪对应的Headers也写上请求的类型但就是不好使
HTTP状态码大致分为5类:
1xx : 消息,这一类型的状态码,代表请求已被接受,需要继续处理。但是一般服务器禁止向客户端发送此类状态码;
2xx : 成功,这一类型的状态码,代表请求已成功被服务器接收、理解、并接受;
3xx : 重定向,这类状态码代表需要客户端采取进一步的操作才能完成请求;
4xx : 请求错误,这类的状态码代表了客户端看起来可能发生了错误,妨碍了服务器的处理;
5xx : 服务器错误,这类状态码代表了服务器在处理请求的过程中有错误或者异常状态发生,也有可能是服务器意识到以当前的软硬件资源无法完成对请求的处理
————————————————
2XX——表明请求被正常处理了
200 OK:请求已正常处理。
204 No Content:请求处理成功,但没有任何资源可以返回给客户端,一般在只需要从客户端往服务器发送信息,而对客户端不需要发送新信息内容的情况下使用。
206 Partial Content:是对资源某一部分的请求,该状态码表示客户端进行了范围请求,而服务器成功执行了这部分的GET请求。响应报文中包含由Content-Range指定范围的实体内容。
3XX——表明浏览器需要执行某些特殊的处理以正确处理请求
301 Moved Permanently:资源的uri已更新,你也更新下你的书签引用吧。永久性重定向,请求的资源已经被分配了新的URI,以后应使用资源现在所指的URI。
302 Found:资源的URI已临时定位到其他位置了,姑且算你已经知道了这个情况了。临时性重定向。和301相似,但302代表的资源不是永久性移动,只是临时性性质的。换句话说,已移动的资源对应的URI将来还有可能发生改变。
303 See Other:资源的URI已更新,你是否能临时按新的URI访问。该状态码表示由于请求对应的资源存在着另一个URL,应使用GET方法定向获取请求的资源。303状态码和302状态码有着相同的功能,但303状态码明确表示客户端应当采用GET方法获取资源,这点与302状态码有区别。
当301,302,303响应状态码返回时,几乎所有的浏览器都会把POST改成GET,并删除请求报文内的主体,之后请求会自动再次发送。
304 Not Modified:资源已找到,但未符合条件请求。该状态码表示客户端发送附带条件的请求时(采用GET方法的请求报文中包含If-Match,If-Modified-Since,If-None-Match,If-Range,If-Unmodified-Since中任一首部)服务端允许请求访问资源,但因发生请求未满足条件的情况后,直接返回304.。
307 Temporary Redirect:临时重定向。与302有相同的含义。
4XX——表明客户端是发生错误的原因所在。
400 Bad Request:服务器端无法理解客户端发送的请求,请求报文中可能存在语法错误。
401 Unauthorized:该状态码表示发送的请求需要有通过HTTP认证(BASIC认证,DIGEST认证)的认证信息。
403 Forbidden:不允许访问那个资源。该状态码表明对请求资源的访问被服务器拒绝了。(权限,未授权IP等)
404 Not Found:服务器上没有请求的资源。路径错误等。
5XX——服务器本身发生错误
500 Internal Server Error:貌似内部资源出故障了。该状态码表明服务器端在执行请求时发生了错误。也有可能是web应用存在bug或某些临时故障。
503 Service Unavailable:抱歉,我现在正在忙着。该状态码表明服务器暂时处于超负载或正在停机维护,现在无法处理请求。
———————————————————————————————————————————
定位问题
一、http 415状态码
是指的对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝。所以就要去看请求头中的content-type填写是否正确
二、从网上搜索了解到原因
根据接口所传参数的不同,post请求的Content-type有四种:
application/x-www-form-urlencoded(默认)
application/xml
application/json
multipart/form-data
三、我所对接的接口
所传的参数是json格式的,代码中没有设置请求头header的Content-Type,默认是application/x-www-form-urlencoded。
四、处理方法
URIBuilder uriBuilder = new URIBuilder(url);
//创建一个http post请求
HttpPost httpPost = new HttpPost(uriBuilder.build());
//设置请求头header
httpPost.setHeader("Content-Type","application/json");
//设置请求数据
httpPost.setEntity(new StringEntity(strJson,"utf-8"));
———————————————————————————————————————————
项目实操定义工具类
一、工具类的使用场景
1、甲方接收url
2、传入内容为json
3、需要解析为xml
4、post请求
二、定义工具类
HttpUtils -- 处理办法,在方法里
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.bankapp.ecds.common.utils.PublicStaticDefineTab;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.CharsetUtils;
import org.json.JSONObject;
/**
* HttpUtil 解析
*/
public class HttpUtil {
private static Log log = LogFactory.getLog(HttpUtil.class);
private static RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(300000)
.setConnectTimeout(300000)
.setConnectionRequestTimeout(300000)
.build();
/**
* http post client
* @param params
* @return
* @throws Exception
*/
public static String sendJsonByPostSanXia(String url, Map<String, String> params) {
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
Map<String, Object> result = new HashMap<String, Object>() ;
CloseableHttpResponse closeableHttpResponse = null;
HttpPost httpPost = null;
InputStreamReader is = null;
BufferedReader br = null;
try {
//建立httpPost
httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.build();
httpPost.setConfig(requestConfig);
//解决办法在这里
//415(Unsupported Media Type) 不支持的类型异常
httpPost.addHeader("Content-Type","application/json");
//加入参数
JSONObject jsonParams = new JSONObject();
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
jsonParams.put(entry.getKey(), entry.getValue());
}
}
httpPost.setEntity(new StringEntity(jsonParams.toString(), Charset.forName("UTF-8")));
log.info("http发送內容: " + jsonParams);
//发起请求
//httpclient.
closeableHttpResponse = httpclient.execute(httpPost);
//获取请求返回
HttpEntity entity = closeableHttpResponse.getEntity();
//获取编码标识
String contentType = entity.getContentType() != null ? entity.getContentType().getValue() : null;
String contentType2 = contentType != null ? (contentType.split(";").length > 1 ? contentType.split(";")[1].trim() : "") : "";
String charset = contentType2.split("=").length > 1 ? contentType2.split("=")[1] : "UTF-8";
is = new InputStreamReader(entity.getContent(),charset);
//解析返回内容
br = new BufferedReader(is);
StringBuilder sb = new StringBuilder();
String len =null;
while ((len = br.readLine()) != null) {
sb.append(len);
}
log.info("http返回內容: " + sb);
//ת为json格式
String back = sb.toString().replace("\"{", "{").replace("}\"", "}");
log.info("返回json内容:" + back);
return back;
} catch (Exception e) {
log.error(e.getMessage(), e);
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
log.error("关闭BufferedReader异常:",e);
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
log.error("关闭InputStreamReader异常:",e);
}
}
if (closeableHttpResponse != null) {
try {
closeableHttpResponse.close();
} catch (IOException e) {
log.error("closeableHttpResponse:",e);
}
}
if (httpPost != null) {
httpPost.releaseConnection();
}
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
log.error("httpclient:",e);
}
}
}
return "{RespCode:'9999',RespDesc:'通讯异常'}";
}
}
三、ServiceImpl层方法调用(SpringBoot框架,大家能看懂就好)
/**
* 跨系统调用
* @description:
* @author: wyd
* @time: 2022/6/20 15:04
**/
@Service
@Log4j2
public class AccountCallServiceImpl implements IAccountCallService {
@Autowired
private TranLogService tranLogService;//日志记录
@Override
public void toSendCoreAccount(AcctCoreFlow coreFlow)throws Exception{
//写 调用会计的 直连发送逻辑
//拼接报文
Map<String,String> reqMap = new HashMap<>();
reqMap.put("mode","raw");
reqMap.put("raw", JSON.toJSONString(coreFlow));
//甲方参数,看情况而定,需要传什么,可劲往里边塞就好
reqMap.put("pubChnl","00");//渠道
reqMap.put("pubSysNo","CBCTS");//系统标识码
reqMap.put("pubSrcAddrInfo","192.168.15.91");
reqMap.put("pubTellCd","W00005");
reqMap.put("pubMacBrnNo","PTL");
//重点来了
//解析xml格式
String respStr = HttpUtil.sendJsonByPostSanXia("http://*.***.***.**:6026",reqMap);
log.info("解析xml格式"+respStr);
//日志存储
TransLogCustomer tranLog = this.tranLogService.toStartTran(TransLogConstants.chnnl_02,"CBCTS",coreFlow.getTrnJnno(),coreFlow);
log.info(JSON.toJSONString("日志存储"+tranLog));
}
}