SparkJava的GET处理程序在对长整型进行序列化后返回NULL。

huangapple go评论76阅读模式
英文:

SparkJava GET Handler Returning NULL After Serialization of Long

问题

我正在尝试使用SparkJava编写一个简单的服务器,但在使用Gson序列化长整型并将JSON传输到GET处理程序内的OkHttp客户端程序时遇到了相当大的困难。服务器返回NULL,更具体地说,response.body.string() 为

<html><body><h2>404 Not found</h2></body></html>

对于这个问题有什么想法吗?谢谢。

以下是GET处理程序:

get("routingEngine/getDefaultRoute/distance", (request,response) ->{
    response.type("application/json");
    long distance = 100;
    return gson.toJson(distance);
});

以下是进行简单请求的客户端代码(请忽略与请求一起传递的参数(requestParameters),它们只是向不相关的前置过滤器提供信息):

// 构建 URL
HttpUrl url = new HttpUrl.Builder()
.scheme("http")
.host("127.0.0.1")
.port(4567)
.addPathSegment("routingEngine")
.addPathSegment("getDefaultRoute")
.addPathSegment("distance")
.build();

// 构建请求
Request getDefaultRouteDistanceRequest = new Request.Builder()
.url(url)
.post(RequestBody.create(JSON,gson.toJson(requestParameters)))
.build();

// 发送请求
Call getDefaultRouteDistanceCall = httpClient.newCall(getDefaultRouteDistanceRequest);
Response getDefaultRouteDistanceResponse = getDefaultRouteDistanceCall.execute();

// 解析响应
// 测试
System.out.println(getDefaultRouteDistanceResponse.body().string());

最后一行导致以下输出:

<html><body><h2>404 Not found</h2></body></html>
英文:

I'm trying to write a simple server with SparkJava but I am having considerable difficulty serialize a long using Gson and transmitting the JSON to an OkHttp client program inside a GET handler. The server returns NULL, more specifically response.body.string() is

&lt;html&gt;&lt;body&gt;&lt;h2&gt;404 Not found&lt;/h2&gt;&lt;/body&gt;&lt;/html&gt;

Any ideas on what the issue could be? thanks.

Here is the GET handler:

get(&quot;routingEngine/getDefaultRoute/distance&quot;, (request,response) -&gt;{
    response.type(&quot;application/json&quot;);
    long distance = 100;
    return gson.toJson(distance);
});

Here is the client code making the simple request (please disregard the parameters (requestParameters) being passed in along with the request, they just provide information to an irrelevant before filter):

    // build url
    HttpUrl url = new HttpUrl.Builder()
    .scheme(&quot;http&quot;)
    .host(&quot;127.0.0.1&quot;)
    .port(4567)
    .addPathSegment(&quot;routingEngine&quot;)
    .addPathSegment(&quot;getDefaultRoute&quot;)
    .addPathSegment(&quot;distance&quot;)
    .build();

    // build request
    Request getDefaultRouteDistanceRequest = new Request.Builder()
    .url(url)
    .post(RequestBody.create(JSON,gson.toJson(requestParameters)))
    .build();

    // send request
    Call getDefaultRouteDistanceCall = httpClient.newCall(getDefaultRouteDistanceRequest);
    Response getDefaultRouteDistanceResponse = getDefaultRouteDistanceCall.execute();
    
    // parse response
    // testing
    System.out.println(getDefaultRouteDistanceResponse.body().string());

The last line leads to the following output

&lt;html&gt;&lt;body&gt;&lt;h2&gt;404 Not found&lt;/h2&gt;&lt;/body&gt;&lt;/html&gt;

答案1

得分: 1

你创建的终端是一个GET终端,而所进行的调用是POST。

英文:

The endpoint you created was a GET endpoint and the call being made is for POST.

huangapple
  • 本文由 发表于 2020年9月2日 06:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63696408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定