【问题标题】:Trying to integrate Spring MVC into existing Jersey API, 404 every time尝试将 Spring MVC 集成到现有的 Jersey API 中,每次 404
【发布时间】:2019-05-14 17:41:27
【问题描述】:

我有一个现有的 MVC 应用程序,我将其用作将 Spring-mvc 集成到此 API 的指南。一切的配置都非常相似,但我无法让它返回资源。

我认为这是可能的,因为我在 C# 中做过类似的事情。我正在使用 Intellij,所以我认为只需添加框架支持就足够了

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.valassis</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>



    <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>/test/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

调度程序-servlet

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.valassis.rest.client" />

    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

控制器

package com.valassis.rest.client;
import com.valassis.rest.DalimGetPropertyValues;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ClientController {
    String message = "Welcome to Spring MVC!";
    private String environment;
    private DalimGetPropertyValues properties = new DalimGetPropertyValues();
    {
        this.environment = properties.getEnvironemnt();
    }
    // Renders the Homepage
    @RequestMapping("/")
    public String showEnviornment(Model model) {
        model.addAttribute("message", returnEnvironment());
        model.addAttribute("name", environment);
        return "index";
    }
    private String returnEnvironment() {
        String url;
        switch (environment) {
            case "dev5.0":
                url = "Currently in dev 5.0: " + properties.getDev2Url();
                break;
            case "newDev5.0":
                url = "Currently in newDev 5.0: " + properties.getNewDev2Url();
                break;
            default:
                url = "Currently in Prod 5.0: " + properties.getPrd2Url();
        }
        return url;
    }
}

index.jsp

<html>
<body>
    <h2>Jersey RESTful Web Application!</h2>
    <p><a href="webapi/myresource">Jersey resource</a>
    <p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
    for more information on Jersey!
</body>
</html>

编辑: 这是我从服务器得到的响应: org.springframework.web.servlet.DispatcherServlet.noHandlerFound 在名为“dispatcher”的 DispatcherServlet 中找不到具有 URI [/test/] 的 HTTP 请求的映射

【问题讨论】:

  • 您使用的 url 是什么?
  • 在Tomcat9上测试,我试过localhost:8090/test/
  • 试试:localhost:8090/your_proj_name/
  • 不走运。使用我提到的链接,服务器返回:org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/test/] in DispatcherServlet with name 'dispatcher'
  • 对 Jersey 使用与 /* 不同的基本路径 (url-pattern)。这是主要问题。如果您使用它,它将接受 all 请求;甚至是那些用于你的 Spring URL 的。

标签: java spring-mvc jersey http-status-code-404 web.xml


【解决方案1】:

您需要在web.xml 中定义dispatcher-servlet。您已在 dispatcher-servlet 而不是 web.xml 中定义它

这应该出现在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>/test/*</url-pattern>
</servlet-mapping>

还要在你的dispatcher-servlet.xml中添加以下内容来表明mvc应用是注解驱动的

<mvc:annotation-driven />

【讨论】:

  • 糟糕!我把标签倒过来了。这就是我在项目中的方式
  • 将 &lt;mvc:annotation-driven /&gt; 添加到您的 dispatcher-servlet。
  • 是的,我已经做到了。我仍然无法弄清楚如何到达我的视野。我在这里看到了一些线程,该站点上有一个名为 jersey-spring4 的罐子。我将它添加到我的 pom 中,运行程序后,这是我得到的错误: java.lang.NoClassDefFoundError: Could not initialize class org.jvnet.hk2.internal.ServiceLocatorImpl org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl .initialize(ServiceLocatorGeneratorImpl.java:66) org.jvnet.hk2.external.generator.ServiceLocatorGeneratorImpl.create(ServiceLocatorGeneratorImpl.java:98)
  • 不太清楚为什么需要添加 jersey 依赖项。
  • 另外,我认为您不需要在 web.xml 中添加 contextConfigLocation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2018-10-23
  • 2012-10-28
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
相关资源
最近更新 更多