postgresql 请求的路由尚未在 Spark 中映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32643505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The requested route has not been mapped in Spark
提问by Alex Rodriguez
I want to do something to sign up users with spark+java+hibernate+postgres
我想做点什么用spark+java+hibernate+postgres来注册用户
This is my code:
这是我的代码:
post("/registrar", (request, response) -> {
EntityManagerFactory emf = Persistence.
createEntityManagerFactory("compradorcitoPU");
EntityManager em = emf.createEntityManager();em.getTransaction().begin();
em.persist(u);
em.getTransaction().commit();
em.close(); return null; });
but this error shows up:
但出现此错误:
INFO spark.webserver.MatcherFilter - The requested route [/registrarnull] has not been mapped in Spark
信息 spark.webserver.MatcherFilter - 请求的路由 [/registrarnull] 尚未在 Spark 中映射
回答by Howard Roark
I had a similar problem. The items I'm returning are large and I wanted to write them out over stream. So, my software looked like this:
我有一个类似的问题。我要返回的项目很大,我想通过流将它们写出来。所以,我的软件看起来像这样:
post("/apiserver", "application/json", (request, response) -> {
log.info("Received request from " + request.raw().getRemoteAddr());
ServerHandler handler = new ServerHandler();
return handler.handleRequest(request, response);
});
In my handler, I got the raw HttpResponse object, opened its OutputStream and wrote over it like so:
在我的处理程序中,我得到了原始的 HttpResponse 对象,打开它的 OutputStream 并像这样写它:
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.raw().getOutputStream(), records);
Since I knew I had written over the OutputStream what the caller had asked for at that point (or an error), I figured I could just return null. My program worked fine. Spark would route the request to my handler as expected. And, since I was writing over the raw OutputStream, I was getting back what was expected on the client side. But, I kept seeing the message '/apiserver route not defined' in my server logs.
因为我知道我已经在 OutputStream 上写了调用者当时要求的内容(或错误),我想我可以只返回 null。我的程序运行良好。Spark 会按预期将请求路由到我的处理程序。而且,由于我是在原始 OutputStream 上写的,所以我得到了客户端的预期。但是,我一直在我的服务器日志中看到消息“/apiserver 路由未定义”。
In looking at the Spark documentation, it says:
在查看 Spark 文档时,它说:
The main building block of a Spark application is a set of routes. A route is made up of three simple pieces:
A verb (get, post, put, delete, head, trace, connect, options)
A path (/hello, /users/:name)
A callback (request, response) -> { }
Spark 应用程序的主要构建块是一组路由。一条路线由三个简单的部分组成:
动词(get、post、put、delete、head、trace、connect、options)
路径 (/hello, /users/:name)
回调(请求,响应)-> { }
Obviously Spark does not know what you wrote over the raw HttpResponse and as a web-server, you should be providing some response to callers. So, if your response is null, you haven't fulfilled the requirements of providing a callback and you get the error that there's no map found even if Spark behaved as expected otherwise. Just return a response (null is not a response, "200 OK" is) and the error will go away.
显然 Spark 不知道你在原始 HttpResponse 上写了什么,作为一个网络服务器,你应该向调用者提供一些响应。因此,如果您的响应为空,则您还没有满足提供回调的要求,并且即使 Spark 的行为符合预期,您也会收到找不到地图的错误。只需返回一个响应(null 不是响应,“200 OK”是),错误就会消失。
[Edit] Spelling and grammar.
[编辑] 拼写和语法。
回答by error2go
do not "return null" instead return the empty string or something
不要“返回空值”而是返回空字符串或其他东西
回答by sontheimer
As explained in the comments of this issue, SparkJava considers that returning null means the route has not been mapped and therefore it logs the error message and replies a response with 404 status.
正如此问题的评论中所解释的,SparkJava 认为返回 null 意味着该路由尚未映射,因此它会记录错误消息并以 404 状态回复响应。
To avoid such behaviour you have to return a String (possibly empty). The error message will disappear and a response with the String as body and 200 status will be replied.
为了避免这种行为,您必须返回一个字符串(可能为空)。错误消息将消失,并回复以字符串为正文和 200 状态的响应。
回答by MonoThreaded
In my case, I had to implement the options request to please the preflight CORS check:
就我而言,我必须实施选项请求以取悦预检 CORS 检查:
options("/*", (request,response)->{
String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
}
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
if(accessControlRequestMethod != null){
response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
}
return "OK";
});