2017-08-01 271 views
0

我試圖實現Spring MVC應用程序,將它部署在JBoss上並能夠通過我的瀏覽器調用它。未找到Spring MVC完整代碼請求映射

我的WebConfig:

package com.heatmanofurioso.concertlivecheck.webapp; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.ContextLoaderListener; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration; 

public class WebConfig implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 
    //Defining web xml 
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
    rootContext.register(SpringApplicationConfig.class); 
    rootContext.setServletContext(servletContext); 

    //Registering servlet 
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(rootContext)); 
    dispatcher.setLoadOnStartup(1); 
    dispatcher.addMapping("/"); 

    servletContext.addListener(new ContextLoaderListener(rootContext)); 
} 
} 

的jboss-web.xml中

<?xml version="1.0" encoding="UTF-8" ?> 
<jboss-web> 
    <context-root>CLC</context-root> 
</jboss-web> 

登錄控制器

package com.heatmanofurioso.concertlivecheck.plugin.controller; 

import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class LoginController { 

@RequestMapping(value = "/test", method = RequestMethod.GET) 
public String greeting5(@RequestParam(value = "name", defaultValue = "World1") String name) { 
    return "Test" + name; 
} 
} 

SpringApplicationConfig

package com.heatmanofurioso.concertlivecheck.webapp; 

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@EnableWebMvc 
@Configuration 
@ComponentScan(basePackages = { 
"com.heatmanofurioso.concertlivecheck", 
"com.heatmanofurioso.concertlivecheck.plugin"}) 
public class SpringApplicationConfig extends WebMvcConfigurerAdapter { 

@Override 
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
    configurer.enable(); 
} 
} 

我的春天版本都是最新的4.x的版本,並在我的主要POM 我的webapp的POM

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<packaging>war</packaging> 
<artifactId>webapp</artifactId> 
<version>1.0-SNAPSHOT</version> 

<parent> 
    <groupId>com.heatmanofurioso.concertlivecheck</groupId> 
    <artifactId>ConcertLiveCheck</artifactId> 
    <version>1.0-SNAPSHOT</version> 
</parent> 

<dependencies> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
    </dependency> 
    <!--Project modules--> 
    <dependency> 
     <groupId>com.heatmanofurioso.concertlivecheck</groupId> 
     <artifactId>utils</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.heatmanofurioso.concertlivecheck</groupId> 
     <artifactId>database</artifactId> 
     <type>pom</type> 
    </dependency> 
    <dependency> 
     <groupId>com.heatmanofurioso.concertlivecheck</groupId> 
     <artifactId>service</artifactId> 
     <type>pom</type> 
    </dependency> 
    <dependency> 
     <groupId>com.heatmanofurioso.concertlivecheck</groupId> 
     <artifactId>plugin</artifactId> 
    </dependency> 
</dependencies> 

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.4</version> 
       <configuration> 
        <warName>${parent.artifactId}</warName> 
        <outputDirectory>${project.build.directory}/../../../../../deployments/</outputDirectory> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.8</version> 
       <executions> 
        <execution> 
         <id>install</id> 
         <phase>install</phase> 
         <goals> 
          <goal>sources</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </pluginManagement> 
</build> 

<properties> 
    <maven.compiler.source>${java.version}</maven.compiler.source> 
    <maven.compiler.target>${java.version}</maven.compiler.target> 
</properties> 
</project> 

我的項目結構中的所有聲明: enter image description here

我有一個例子JSP文件,我的瀏覽器可以查找和讀取本地主機:8080/CLC 但是,當我試圖進入一個映射的路線,像本地主機:8080/CLC /測試 我收到了404個

什麼可能是根任何提示對我的問題?

P.S. JBoss的輸出:enter image description here

另外,如果不知道它的相關性,但春天的版本是4.3.10釋放,和JBoss被廣泛

更新: 所以,我降級春季版本4.3.7釋放和現在一切工作正常,關於爲什麼?我也證明是行家降級之前,捆綁每一個部件具有相同的彈簧版本

+0

能否請您試試這個:dispatcher.addMapping(「/ *」);只是爲了確保Dispatcher Servlet能夠處理您的所有請求。 –

+0

'localhost:8080/CLC/test?name = test'怎麼辦? – StanislavL

+0

@StanislavL假設參數與請求本身沒有任何關係,變量將變爲空。還是我錯了?無論如何,謝謝你,我試過了。但沒有好處:( –

回答

0

你並不需要公開應用程序的名稱,儘量只是做一個GET請求:

http://localhost:8080/test 

如果你想多了一個層次添加到您的網址,你可以這樣做:

@RequestMapping(value = "CLC/test", method = RequestMethod.GET) 
public String greeting5(@RequestParam(value = "name", defaultValue = "World1") String name) { 
    return "Test" + name; 
} 
+0

仍然沒有找到。 另外,JBoss不應該將我的上下文映射到我的ip上的第一個元素,然後將映射放在它旁邊?那麼,context =「CLC」,mapping =「test」,路徑「localhost:8080/CLC/test」? –

+0

你能確定你的應用程序的運行狀態嗎? – andreybleme

+0

是的,因爲我的index.jsp可以在「localhost:8080/CLC」找到,並且我在其中插入了「Hello World」文本。還添加了一個打印的JBoss輸出 –

0

最新的Spring Security使用彈簧芯4.3.7,而我是用彈簧芯4.3.10。

這些圖書館在我的罐子裏有衝突。

我降級我的春天核心4.3.7,一切工作細

+0

我有同樣的問題,降級到4.3.9解決了我的問題,但是有什麼問題? – ashkanr