不知道我在这里错过了什么......
sample Controller :
@Controller
@RequestMapping(value="/schedule/*")
public class ScheduleController {
@RequestMapping(value="/job", method=RequestMethod.GET)
@ResponseBody
public String test(@RequestParam (value = "name", required = false) String name) throws Exception{
System.out.println("name:" + name);
return "Yes, I got it, " + name;
}
}
web.xml
<display-name>JS</display-name>
<!-- Spring MVC -->
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/WEB-INF/spring/js-rest.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
js-rest.xml 文件
<context:component-scan base-package="com.test.js.controller"> </context:component-scan>
<context:annotation-config />
毕业典礼:
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
compile 'org.springframework:spring-core:3.0.5.RELEASE'
compile 'org.springframework:spring-beans:3.0.5.RELEASE'
compile 'org.springframework:spring-context:3.0.5.RELEASE'
compile 'org.springframework:spring-webmvc:3.0.5.RELEASE'
compile 'org.springframework:spring-aop:3.0.5.RELEASE'
compile 'org.springframework:spring-web:3.0.5.RELEASE'
providedCompile 'javax.servlet:servlet-api:2.5'
runtime 'javax.servlet:jstl:1.1.2'
}
jettyRunWar.contextPath = ''
Controller 在
src\main\java\com\test\js\controller
文件夹web.xml
在 src\main\webapp\WEB-INF\
文件夹js-rest.xml
在 src\main\webapp\WEB-INF\spring\
文件夹试过了
localhost:8080\schedule\job
localhost:8080\<projectname>\schedule\job
localhost:8080\job
localhost:8080\<project-name>\job
我得到的只是 404 未找到。
我在这里错过了什么吗?
最佳答案
我认为你不应该有 *
在 Controller 路径中。试试这个,看看是否有效。
@Controller
@RequestMapping(value="/schedule") // removed the /*
public class ScheduleController {
@RequestMapping(value="/job", method=RequestMethod.GET)
@ResponseBody
public String test(@RequestParam (value = "name", required = false) String name) throws Exception{
System.out.println("name:" + name);
return "Yes, I got it, " + name;
}
}
此外,如果您想要像项目名称这样的捕获组,您最好使用
@Pathvariable
像这样的注释。 @RequestMapping(value="/{projectName}/job", method=RequestMethod.GET)
@ResponseBody
public String test(@Pathvariable("projectName") String projectName) throws Exception{
...
}
关于Spring MVC 映射-未找到 404 错误-使用 Gradle 和 Jetty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34296417/