1. 程式人生 > >Postman測試http請求返回415狀態碼的解決

Postman測試http請求返回415狀態碼的解決

首先記錄一下返回結果

<!doctype html>
<html lang="en">
<head><title>HTTP Status 415 – Unsupported Media Type</title>
<style type="text/css">
H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} 
H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} 
BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} 
B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} 
P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}
A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}
</style>
</head>
<body>
<h1>HTTP Status 415 – Unsupported Media Type</h1>
<hr class="line" /><p><b>Type</b> Status Report</p>
<p><b>Message</b> Unsupported Media Type</p>
<p><b>Description</b> The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.</p>
<hr class="line" />
<h3>Apache Tomcat/8.0.53</h3>
</body></html>

postman引數如下圖

 使用https請求並攜帶一組formData向eclipse後臺請求資料,結果返回狀態碼415

415狀態碼 Unsupported Media Type 伺服器無法處理請求附帶的媒體格式

處理該請求的原始碼是:

@POST
	@Path("calc")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes({MediaType.APPLICATION_XHTML_XML,MediaType.APPLICATION_XML/*,MediaType.APPLICATION_FORM_URLENCODED*/})
	public String getCalculationResult(@FormParam("num1") String num1, @FormParam("num2")String num2){
		Result r = new Result().success();
		r.setData(NumberUtils.toInt(num1)+NumberUtils.toInt(num2));
		return r.toString();
	}

執行該方法時的日誌記錄

八月 29, 2018 2:47:19 下午 org.glassfish.jersey.filter.LoggingFilter log
資訊: 1 * LoggingFilter - Request received on thread http-nio-443-exec-13
1 > POST https://localhost/master/rest/jersey/calc
1 > content-type: application/x-www-form-urlencoded
1 > cache-control: no-cache
1 > postman-token: 43615349-7d54-45e5-962b-8ed59e3be45d
1 > user-agent: PostmanRuntime/7.1.1
1 > accept: */*
1 > host: localhost
1 > cookie: JSESSIONID=0AD7194FDD008D9919CF1B3B4E15F09C
1 > accept-encoding: gzip, deflate
1 > content-length: 14
1 > connection: keep-alive

八月 29, 2018 2:47:19 下午 org.glassfish.jersey.filter.LoggingFilter log
資訊: 1 * LoggingFilter - Response received on thread http-nio-443-exec-13
1 < 415

可以看到,該條請求的 content-type: application/x-www-form-urlencoded,@Consumes的引數列表中沒有此種類型的變數,即表示該資源不接受此種content-type的請求。(使用的RESTful 風格的jersey框架),增加這種請求媒體型別的支援即可,即去掉原始碼上@Consumes的標籤註釋即可解決。