The requested route has not been mapped in Spark
Asked Answered
T

4

9

I want to do something to sign up users with 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

Tavie answered 18/9, 2015 at 3:44 Comment(1)
I had the same (kind of) error, but mine was with a css file which didn't have a mapped route. I managed to fix the error with the help of this answer. I hope it can help you aswell. (Set the staticFileLocation did the job for me). https://mcmap.net/q/448396/-where-to-put-static-files-for-spark-web-frameworkDevinna
P
10

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:

    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.

In looking at the Spark documentation, it says:

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) -> { }

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.

[Edit] Spelling and grammar.

Pittel answered 26/9, 2015 at 7:46 Comment(0)
B
6

do not "return null" instead return the empty string or something

Bach answered 23/9, 2015 at 16:3 Comment(0)
E
4

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.

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.

Equilibrist answered 4/8, 2017 at 17:38 Comment(0)
H
3

In my case, I had to implement the options request to please the preflight CORS check:

    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";
    });
Haematopoiesis answered 27/3, 2018 at 5:47 Comment(2)
I want to thank myself for reminding me of this solution 5 months down the roadHaematopoiesis
I want to thank yourself for reminding of this solution seven months down the road. This problem bites me every time I use Spark Java.Jecoa

© 2022 - 2024 — McMap. All rights reserved.