Spring MVC 映射-未找到 404 错误-使用 Gradle 和 Jetty

标签 spring rest model-view-controller gradle http-status-code-404

不知道我在这里错过了什么......

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.xmlsrc\main\webapp\WEB-INF\文件夹js-rest.xmlsrc\main\webapp\WEB-INF\spring\文件夹

试过了localhost:8080\schedule\joblocalhost:8080\<projectname>\schedule\joblocalhost:8080\joblocalhost: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/

相关文章:

ruby-on-rails - rails : two models for one table

javascript - 使用 jQuery 从输入控件获取值

java - Controller 类中的 Spring MVC 本地化不起作用

java - 外部服务的创建 API。发送二进制数据

html - MVC 连续排列元素

rest - API 开发 : When to return a BAD REQUEST HTTP Status Code (REST)

java - 其余输出与控制台与 Tomcat localhost 不同

java - HttpMessageNotReadableException : Could not read document: Stream closed;

Spring:必须Service或Controller或Repository有一个接口(interface)

java - 如何在Excel中导出网页