The requested route was not displayed in Spark

I want to do something to register users using 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 appears:

INFO spark.webserver.MatcherFilter - the requested route [/ registrarnull] was not displayed in Spark

+4
source share
4 answers

I had a similar problem. The items I return are large, and I wanted to write them downstream. 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 original HttpResponse object, opened its OutputStream, and wrote above it like this:

    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(response.raw().getOutputStream(), records);

, OutputStream , ( ), , . . , . , OutputStream, , . "/apiserver route not defined" .

Spark :

Spark - . :

(get, post, put, delete, head, trace, connect, options)

(/hello,/users/: name)

(, ) → {}

, Spark , HttpResponse, -, - . , , , , , Spark , . (null - , "200 OK" ), .

[] .

+6

null " -

+1

issue, SparkJava , null , , 404.

To avoid this behavior, you should return a string (possibly empty). The error message will disappear and a response with the status of String as body and 200 will be sent.

+1
source

In my case, I had to implement a parameter request to satisfy the CORS pre-flight 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";
    });
0
source

Source: https://habr.com/ru/post/1607945/


All Articles