Getting error: Route() in Route cannot be applied to String

前端 未结 5 985
栀梦
栀梦 2021-01-05 18:48

I\'m designing a Java based MongoDB app and I\'ve ran into a snag when working with Spark.

package com.tengen;

import spark.Request;
import spark.Response;
         


        
相关标签:
5条回答
  • 2021-01-05 19:15

    Actually I used spark-core-1.1.1.jar it worked fine for me, may be the newer versions of Spark(version 2.0.0) support some different syntax.So if you are using the latest version then you can follow the example given by Mike or just add spark-core-1.1.1.jar to your classpath your example will work fine

    0 讨论(0)
  • 2021-01-05 19:23

    Change the version of Spark in the POM file from the exercise files you download from handout. That fixed issue for me.

    0 讨论(0)
  • 2021-01-05 19:26

    This should probably be posted on the MongoDB class forum, but I ran into a similar issue. Looks like the get method changed from when the course material was produced. The get now requires a path and a Route

    get(path, Route)

    import spark.Request;
    import spark.Response;
    import spark.Route;
    import spark.Spark;
    
    public class HelloWorldSparkStyle {
        public static void main(String[] args){
    
            Spark.get("/", new Route() {
                    public Object handle(final Request request, final Response response){
                    return "Hello World from Spark";
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-01-05 19:27

    Currently, Implementation of Spark Java framework needs to use directory out of Route. So you have to correct your code as follow:

    public static void main(String[] args) {
        spark.Spark.port(PortNumber);
        Spark.get("/", new Route() {
                public Object handle(Request request, Response response) throws Exception {
                    return "This is a sample page";
                }
            });
    }
    

    actually, "/" is the resource to your program. And if you want to change the default spark.Spark.port(PortNumber).

    0 讨论(0)
  • 2021-01-05 19:35

    That would work in Spark 1. In Spark 2 is recommended by Spark the following (source: http://sparkjava.com/news.html):

    import static spark.Spark.*;
    
    public class HelloWorld {
        public static void main(String[] args) {
            get("/", (req, res) -> "Hello World From Spark");
        }
    }
    
    0 讨论(0)
提交回复
热议问题