Postman测试http请求返回415状态码的解决

首先记录一下返回结果



HTTP Status 415 – Unsupported Media Type



HTTP Status 415 – Unsupported Media Type


Type Status Report

Message Unsupported Media Type

Description 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.


Apache Tomcat/8.0.53

postman参数如下图

Postman测试http请求返回415状态码的解决_第1张图片

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

你可能感兴趣的:(Jersey,Postman,http返回码)