【发布时间】: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