【问题标题】:Spring MVC mapping - Not found 404 error - with Gradle and JettySpring MVC 映射-未找到 404 错误-使用 Gradle 和 Jetty
【发布时间】:2016-03-21 16:02:15
【问题描述】:

不确定我在这里缺少什么......

样品控制器:

@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 = ''

控制器位于src\main\java\com\test\js\controller 文件夹中 web.xmlsrc\main\webapp\WEB-INF\ 文件夹中 js-rest.xmlsrc\main\webapp\WEB-INF\spring\ 文件夹中

试过了 localhost:8080\schedule\job localhost:8080\&lt;projectname&gt;\schedule\job localhost:8080\job localhost:8080\&lt;project-name&gt;\job

我得到的只是 404 Not found。

我这里有什么遗漏吗?

【问题讨论】:

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


    【解决方案1】:

    我认为您不应该在控制器路径中有*。试试这个,看看是否有效。

    @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;
       }
    }
    

    另外,如果你想要像项目名称这样的捕获组,你最好使用@Pathvariableannotation 像这样。

       @RequestMapping(value="/{projectName}/job", method=RequestMethod.GET)
       @ResponseBody
        public String test(@Pathvariable("projectName") String projectName) throws Exception{
          ...
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      相关资源
      最近更新 更多