java - spring 映射的 servlet 未在 jetty-maven-plugin 中调用

标签 java maven spring-mvc maven-jetty-plugin

had to migratemaven-jetty-pluginjetty-maven-plugin。当我使用jetty:run启动jetty时,我可以看到上下文已部署,应用程序正在初始化,但未找到servlet。它可以与maven-jetty-plugin一起正常工作。

Java

@RequestMapping(value = "/handshake", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public void performHandShake(@org.springframework.web.bind.annotation.RequestBody String requestData, HttpServletResponse response) throws IOException {

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Maven

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${maven-jetty-plugin.version}</version>

            <configuration>
                <webApp>
                    <contextPath>pos-json</contextPath>
                </webApp>
                <scanIntervalSeconds>0</scanIntervalSeconds>

浏览器http://localhost:8080/

No context on this server matched or handled this request.
Contexts known to this server are:
pos-json ---> o.e.j.m.p.JettyWebAppContext@6c6366cf{pos-json,file:///C:/dev/projekty/pos-backend/src/main/webapp/,AVAILABLE}{file:///C:/dev/projekty/pos-backend/src/main/webapp/}

浏览器http://localhost:8080/pos-json/handshake

HTTP ERROR 404
Problem accessing /pos-json/handshake. Reason:   Not Found

jetty

[INFO] Classes = C:\dev\projekty\pos-backend\target\classes
[INFO] Context path = pos-json
[INFO] Tmp directory = C:\dev\projekty\pos-backend\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:///C:/dev/projekty/pos-backend/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = C:\dev\projekty\pos-backend\src\main\webapp
2017-10-20 14:21:17.907:INFO:oejs.Server:main: jetty-9.4.7.v20170914
2017-10-20 14:21:23.050:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=4520ms
2017-10-20 14:21:23.486:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
2017-10-20 14:21:23.486:INFO:oejs.session:main: No SessionScavenger set, using defaults
2017-10-20 14:21:23.490:INFO:oejs.session:main: Scavenging every 660000ms
2017-10-20 14:21:24.498:INFO:oejshC.pos_json:main: Initializing Spring FrameworkServlet 'dispatcher'
2017-10-20 14:21:39,828 INFO [main] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped "{[/handshake],methods=[POST],produces=[application/json;charset=UTF-8]}" onto public void pos.backend.device.controller.DeviceJsonController.performHandShake(java.lang.String,javax.servlet.http.HttpServletResponse) throws java.io.IOException
2017-10-20 14:21:40,167 INFO [main] o.s.w.s.m.m.a.RequestMappingHandlerAdapter Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Oct 20 14:21:24 CEST 2017]; root of context hierarchy
2017-10-20 14:21:40.783:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@6c6366cf{pos-json,file:///C:/dev/projekty/pos-backend/src/main/webapp/,AVAILABLE}{file:///C:/dev/projekty/pos-backend/src/main/webapp/}
2017-10-20 14:21:41.017:INFO:oejs.AbstractConnector:main: Started ServerConnector@179efe9a{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}

最佳答案

来自documentation关于jetty-maven-plugin的配置似乎在contextPath中他们建议您可能(我必须承认非常模糊)需要添加前导斜杠 "/",以便您的 Maven 插件定义应为:

    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${maven-jetty-plugin.version}</version>

        <configuration>
            <webApp>
                <contextPath>/pos-json</contextPath>
            </webApp>
            <scanIntervalSeconds>0</scanIntervalSeconds>
        </configuration>
    </plugin>

希望这有帮助!

关于java - spring 映射的 servlet 未在 jetty-maven-plugin 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46849304/

相关文章:

java - 交换 JPanel 内容时闪烁

java - Jenkins Windows Slave JNLP 连接错误

Windows 64 位中 Program Files 文件夹的 Java 路径

java - GSS 异常 : No valid credentials provided (Mechanism level: Failed to find any Kerberos tgt)

java - Spring MVC 3 表单验证注释被忽略?

java - 修改颜色选择器面板

java - 如何在 splinter 的测试环境中继续前进?

java - Maven 使用 pomIninclude

java - 使用 Xtext 生成的 Artifact

java - 如何在 Spring Controller 中注入(inject) Cache-Control max-age header ?