我需要使用简单的Rest服务,但是如果我的请求使用Content-type:application / x-www-form-urlencoded,则它们的实现会中断。我需要将其设置为“ application / json”,否则将面临状态415。
我正在使用restlet生产者组件,因为它已经在整个过程中使用,到目前为止,它已经达到了功能性和简单性之间的最佳结合点。迄今为止。
无论如何,尝试在我的路由中设置标头似乎无效,并且我的请求的内容类型仍为application / x-www-form-urlencoded。这是我的测试代码:
from("direct:getImg")
.setHeader(RestletConstants.RESTLET_LOGIN, simple("admin"))
.setHeader(RestletConstants.RESTLET_PASSWORD, simple("admin"))
.setHeader(Exchange.CONTENT_TYPE, simple("application/json"))
.to("restlet:http://requestb.in/12sowlx1?restletMethod=get&throwExceptionOnFailure=false")
我显然错过了一些东西,但是我找不到任何例子。谁能指出正确的方法呢?
谢谢!
在调用restlet并在交换中设置内容类型之前,应先呼叫处理器。像这样:
from("direct:getImg").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_XML);
}
}).to("restlet:http://requestb.in/12sowlx1?restletMethod=get&throwExceptionOnFailure=false");
我已经对其进行了测试,并且可以正常工作。让我知道结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句