警告:在名为';SpringRest&x27;
我试图点击url,但我遇到了404错误&在控制台中,我收到了警告,警告:在名为“SpringRest”的DispatcherServlet中,找不到URI为[/spring mvc/hello]的HTTP请求的映射。 我不确定web.xml的设置。我的代码如下。 任何帮助都会得到报答。提前谢谢 web.xml警告:在名为';SpringRest&x27;,spring,servlets,Spring,Servlets,我试图点击url,但我遇到了404错误&在控制台中,我收到了警告,警告:在名为“SpringRest”的DispatcherServlet中,找不到URI为[/spring mvc/hello]的HTTP请求的映射。 我不确定web.xml的设置。我的代码如下。 任何帮助都会得到报答。提前谢谢 web.xml <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http:/
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
SpringRest-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<mvc:annotation-driven/>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package = "com.wocs" />
<import resource="classpath:/beanfactory/service-bean-config.xml" />
</beans>
可能是
@RequestMapping(“/hello”)
,因为您没有定义用于接收请求的方法类型
试试这个
@GetMapping(value="/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> hello() throws ServiceException
{
return new ResponseEntity<>("Hello............", HttpStatus.OK);
}
@GetMapping(value=“/hello”,products=MediaType.APPLICATION\u JSON\u value)
public ResponseEntity hello()引发ServiceException
{
返回新的ResponseEntity(“Hello………”,HttpStatus.OK);
}
您可以像这样修改控制器类,并告诉我它是否有效
@Controller
public class TestController {
@GetMapping("/hello")
public String hello() throws ServiceException
{
return "Hello............";
}
}
如果这不起作用,你可以试试
@Controller
public class TestController {
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = "application/json")
public String hello() throws ServiceException
{
return "Hello............";
}
}
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>SpringRest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringRest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Web应用程序创建的原型
弹簧座
org.springframework.web.servlet.DispatcherServlet
弹簧座
/
检查您的web.xml文件和SpringRest-servlet.xml文件是否在webapp/web-INF文件夹中,还检查您的控制器java类是否在src/main/java而不是src/main/test中,大多数情况下,目录结构不正确会导致问题。如果仍然不起作用,请告诉我。将此添加到web.xml文件:-
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringRest-servlet.xml</param-value>
</context-param>
<listener>
<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
上下文配置位置
/WEB-INF/SpringRest-servlet.xml
org.springframework.web.context.ContextLoaderListener
您使用的是Spring MVC还是Boot?Spring MVC……强烈建议使用Spring Boot,让您的生活更轻松。使JAR非WAR令牌“/hello”上出现语法错误,MemberValuePairs无效抱歉我的错误,让我修复it@rahulshalgar试试看,你有下面的导入语句吗<代码>导入org.springframework.http.HttpStatus代码>,导入org.springframework.http.ResponseEntity代码>,导入org.springframework.http.MediaType代码>,导入org.springframework.web.bind.annotation.GetMapping
Opps,这是@GetMapping(value=“/hello”,
没有value
mapping。检查控制器java文件是否在src/main/java文件夹中,而不是在src/main/test中,还检查web.xml文件是否与上述文件相同
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringRest-servlet.xml</param-value>
</context-param>
<listener>
<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>