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

tarzanbappa
  • 4,930
  • 22
  • 75
  • 117
Alex Rodriguez
  • 91
  • 1
  • 1
  • 2
  • 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). http://stackoverflow.com/a/28227521/4003055 – Mathieu Brouwers Dec 12 '16 at 17:02

4 Answers4

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.

Community
  • 1
  • 1
Howard Roark
  • 301
  • 1
  • 9
6

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

error2go
  • 61
  • 1
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.

sontheimer
  • 66
  • 4
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";
    });
MonoThreaded
  • 11,429
  • 12
  • 71
  • 102