Spring application with embedded jetty can't find webdefault.xml if running from jar

前端 未结 6 532
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 15:19

I have spring application which uses embedded Jetty instance.

project
   | src
      | controller
      | webapps
          | jsp
          | WEB-INF
                    


        
6条回答
  •  面向向阳花
    2021-02-03 15:50

    I had a similar problem and I solve it with this main class implementation:

    private static final int PORT = 8080;
    private static final String WAR_LOCATION = "src/webapps"; //in your case I guess
    private static final String CONTEXT_PATH = "/movence"; //change it if you want
    
    public static void main(String[] args) throws Exception {
        Server server = new Server();
        WebAppContext context = new WebAppContext();
        SocketConnector connector = new SocketConnector();
    
        setupConnector(connector);
        setupContext(server, context);
        setupServer(server, context, connector);
        startServer(server);
    }
    
    private static void startServer(Server server) throws Exception, InterruptedException {
        server.start();
        server.join();
    }
    
    private static void setupServer(Server server, WebAppContext context, SocketConnector connector) {
        server.setConnectors(new Connector[] { connector });
        server.addHandler(context);
    }
    
    private static void setupConnector(SocketConnector connector) {
        connector.setPort(PORT);
    }
    
    private static void setupContext(Server server, WebAppContext context) {
        context.setServer(server);
        context.setContextPath(CONTEXT_PATH);
        context.setWar(WAR_LOCATION);
    }
    

提交回复
热议问题