使用 Java restlet 发布到服务器



我正在尝试使用 restlet 向服务器发布 JSONO bject。当我尝试使用 POSTMAN 发布时,它能够发布,但是当我尝试使用 restlet 从 java 代码发布时,我收到错误为:

找不到此对象的转换器 和错误请求 (400) - 由于语法格式不正确,服务器无法理解请求。

我的代码是:

ClientResource resource = new ClientResource(localHost + "/path");
//resource.setMethod(Method.POST);
resource.setAttribute("Authorization", authHdr); //auth code passed to the function 
resource.setAttribute("Accept", "application/json");
resource.setAttribute("Content-Type", "application/json");
Map<Integer, JSONObject> assetGrpList = networks; //map I pass to function
for(Iterator<Map.Entry<Integer, JSONObject>> it = assetGrpList.entrySet().iterator(); it.hasNext();)
{
    Map.Entry<Integer, JSONObject> mapEntry = it.next();
    JSONObject network = mapEntry.getValue();
    JSONObject jsonobj = new JSONObject();
    jsonobj.put("managedSiteId", siteId);
    jsonobj.put("assetGrpExternalId", network.get("NetworkID").toString());
    jsonobj.put("assetGrpName", network.get("NetworkName"));
    jsonobj.put("geoLocationInfoId", 1);
    log.info(jsonobj.toString());
    Representation response = resource.post(jsonobj, MediaType.APPLICATION_JSON); 
    if(resource.getResponse().getStatus().getCode() > 200) {
        try {
            log.info(response.getText());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

org.restlet.resource.ResourceException: Bad Request
at org.restlet.resource.ClientResource.doError(ClientResource.java:590)
at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1153)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1048)
at org.restlet.resource.ClientResource.handle(ClientResource.java:1023)
at org.restlet.resource.ClientResource.post(ClientResource.java:1485)
at org.restlet.resource.ClientResource.post(ClientResource.java:1424)
at com.waygum.mbaas.connectors.helper.MbaasRestClient.postAssetGrp(MbaasRestClient.java:531)
at com.waygum.mbaas.connectors.MbaasConnectorsBootRouter$1.process(MbaasConnectorsBootRouter.java:229)
at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)
at org.apache.camel.component.sparkrest.CamelSparkRoute.handle(CamelSparkRoute.java:46)
at spark.SparkBase$1.handle(SparkBase.java:311)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:159)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:60)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:499)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:745)

我认为您以错误的方式创建请求。您应该使用 Restlet API 来执行此操作。

ClientResource resource = new ClientResource(localHost + "/path");
resource.setAttribute("Authorization", authHdr); //auth code passed to the function 
// JSON payload and Content-Type header
JSONObject jsonObj = (...)
JsonRepresentation repr = new JsonRepresentation(jsonObj);
// Authorization header
String pAccessToken = "some token";
ChallengeResponse challengeResponse = new ChallengeResponse(
                  new ChallengeScheme("", ""));
challengeResponse.setRawValue(pAccessToken);
resource.setChallengeResponse(challengeResponse);
// Request and Accept header
resource.post(jsonObj, MediaType.APPLICATION_JSON);

表示形式自动包含application/json内容类型,因为它是 JSON 内容类型。第二个参数定义Accept标头的值。

关于Authorization标头,HTTP需要一个前缀,例如Basic。这就是为什么 Restlet 需要在其 API 中有一个。作为解决方法,您可以传递空前缀。有关更多详细信息,请参阅此答案:

  • 如何将带有密钥的 Restlet 客户 HTTP 标头指定为"授权"

相关内容

  • 没有找到相关文章

最新更新