Postman测试http请求返回415状态码的解决 - Go语言中文社区

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的标签注释即可解决。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/LJX_ahut/article/details/82182954
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-31 01:01:33
  • 阅读 ( 724 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢