#spring-mvc #tomcat #java-8 #spring-tool-suite #spring-4
#spring-mvc #tomcat #java-8 #spring-tool-suite #spring-4
Вопрос:
Я испытываю странную ошибку. Мое приложение работает на одной машине, но тот же URL выдает 404 на другой машине с той же конфигурацией. Технология, которую я использую
Spring4.1.1.6
Apache Tomcat 8.0.37
JDK 1.8_178
Мой POM:
<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>
<groupId>com.xxx.xxxx</groupId>
<artifactId>GeoApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Geo api</name>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.1.6.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<!-- Missing -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- Spring Security -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Я использую конфигурацию Spring на основе Java как:
Config.Java
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration //Marks this class as configuration
//Specifies which package to scan
@ComponentScan("com.xx.xxxx.api")
//Enables Spring's annotations
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
И
public class WebInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
Контроллер REST выглядит так:
@RestController
public class TestRestController {
@RequestMapping("/sample")
public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return name;
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ResponseEntity<String[]> listAllUsers(HttpServletResponse response, HttpServletRequest request ) {
String[] user = new String[10];
user[0]= "ram";
user[1] = "shyam";
if(user.length <=0 ){
return new ResponseEntity<String[]>(HttpStatus.NO_CONTENT);
//TODO: discuss if the status has to be HttpStatus.NOT_FOUND
}
return new ResponseEntity<String[]>(user, HttpStatus.OK);
}
}
The Apache tomcat running with STS, The log difference on application startup are:
Working application:
Oct 06, 2016 10:56:34 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:GeoApi' did not find a matching property.
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.37
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Sep 1 2016 10:01:52 UTC
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.37.0
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:Program FilesJavajdk1.8.0_72jre
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_72-b15
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: F:projectssts-workspace.metadata.pluginsorg.eclipse.wst.server.coretmp1
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: F:Softwarestsapache-tomcat-8.0.37
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=F:projectssts-workspace.metadata.pluginsorg.eclipse.wst.server.coretmp1
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=F:Softwarestsapache-tomcat-8.0.37
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=F:projectssts-workspace.metadata.pluginsorg.eclipse.wst.server.coretmp1wtpwebapps
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=F:Softwarestsapache-tomcat-8.0.37endorsed
Oct 06, 2016 10:56:34 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Oct 06, 2016 10:56:34 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:Program FilesJavajdk1.8.0_72bin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:/Program Files/Java/jre1.8.0_72/bin/server;C:/Program Files/Java/jre1.8.0_72/bin;C:/Program Files/Java/jre1.8.0_72/lib/amd64;C:ProgramDataOracleJavajavapath;C:Program FilesHaskellbin;C:Program FilesHaskell Platform7.10.3libextralibsbin;C:Program FilesHaskell Platform7.10.3bin;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesHewlett-PackardSimplePass;C:Program Files (x86)Common Fileslenovoeasyplussdkbin;C:Program FilesMicrosoft SQL Server110ToolsBinn;F:projectsDivelingSoftwaresgrails-2.1.1bin;C:Program FilesTortoiseGitbin;C:Program Files (x86)Gitbin;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program Files (x86)Microsoft SDKsTypeScript1.0;C:Program FilesMicrosoft SQL Server120ToolsBinn;C:wampbinphpphp5.5.12;C:wampbinmysqlmysql5.6.17bin;c:Program FilesIntelWiFibin;c:Program FilesCommon FilesIntelWirelessCommon;C:ProgramDatachocolateybin;C:Program Files (x86)WinMerge;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program Files (x86)Windows LiveShared;C:Program FilesHaskell Platform7.10.3mingwbin;C:Program Files (x86)SkypePhone;C:Program FilesMicrosoftWeb Platform Installer;C:Program Filesnodejs;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Filesdotnet;C:Program FilesAmazonAWSCLI;C:UsersRupendra SharmaAppDataRoamingcabalbin;c:Program FilesIntelWiFibin;c:Program FilesCommon FilesIntelWirelessCommon;C:UsersRupendra SharmaAppDataLocalatombin;C:UsersRupendra SharmaAppDataRoamingnpm;F:Softwaremysql-connector-java-5.1.39;C:UsersRupendra SharmaDesktopAnil support;F:Softwarestsapache-maven-3.3.9bin;F:Softwarestssts-bundlests-3.6.4.RELEASE;;.
Oct 06, 2016 10:56:34 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 06, 2016 10:56:35 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 06, 2016 10:56:35 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Oct 06, 2016 10:56:35 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 06, 2016 10:56:35 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1008 ms
Oct 06, 2016 10:56:35 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Oct 06, 2016 10:56:35 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.37
Oct 06, 2016 10:56:35 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [119] milliseconds.
Oct 06, 2016 10:56:40 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 06, 2016 10:56:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [com.xx.XXXX.api.configuration.WebInitializer@53e8262c]
log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
log4j:WARN Please initialize the log4j system properly.
Oct 06, 2016 10:56:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Oct 06, 2016 10:56:50 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 06, 2016 10:56:50 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Oct 06, 2016 10:56:50 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 15719 ms
На другом компьютере, где тот же URL выдает исключение 404:
Oct 06, 2016 11:38:32 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:GeoApi' did not find a matching property.
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.37
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Sep 1 2016 10:01:52 UTC
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.37.0
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Linux
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 3.16.0-77-generic
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /home/claritus/jdk1.8.0_102/jre
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_102-b14
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /home/claritus/Documents/workspace-sts-3.8.1.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /home/claritus/apache-tomcat-8.0.37
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/claritus/Documents/workspace-sts-3.8.1.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/home/claritus/apache-tomcat-8.0.37
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/home/claritus/Documents/workspace-sts-3.8.1.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/home/claritus/apache-tomcat-8.0.37/endorsed
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Oct 06, 2016 11:38:32 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Oct 06, 2016 11:38:32 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 06, 2016 11:38:32 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 06, 2016 11:38:32 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Oct 06, 2016 11:38:32 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Oct 06, 2016 11:38:32 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 832 ms
Oct 06, 2016 11:38:32 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Oct 06, 2016 11:38:32 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.37
Oct 06, 2016 11:38:33 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 06, 2016 11:38:33 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Oct 06, 2016 11:38:33 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 538 ms
Я перепробовал все, чтобы устранить проблему:
- Maven чистая установка
- Проект чистый
- Запуск STS в режиме администратора
- запуск-остановка apache
Комментарии:
1. Также я использую JDK1.8 в качестве Apache JRE. Я попробовал это как возможное исправление, но оно не сработало.
2. какой URL-адрес вы пытаетесь использовать?
3. localhost: 8080 / GeoAPI без каких-либо изменений localhost: 8080/GeoAPI / swagger-api-doc1.0