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参数如下图

Postman测试http请求返回415状态码的解决详解程序员

 使用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框架),增加这种请求媒体类型的支持即可,[email protected]

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/2471.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论