服务器为什么不响应GET请求

时间:2019-11-06 16:31:41

标签: java spring spring-boot

我正在使用Spring的某些库在Java中开发Web应用程序。当我将“获取”请求发送到“ studentsystem2 / students”时,服务器Tomcat不会收到该请求,并且它不会响应。我收到404 NOT FOUND错误。基本上没有任何反应。我找不到错误所在。我是否缺少配置文件或其他内容?

我已经创建了一个Jax Rs应用程序,使用JPA Hibernate配置了它。我还没有为上下文根定义任何文件或类。

StudentResource.java

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ikubinfo.model.StudentModel;
import com.ikubinfo.service.StudentService;

@RestController
@RequestMapping(value="/students", consumes="application/json", produces="application/json")
public class StudentResource {

    @Autowired
    private StudentService studentService;
    public StudentResource () {

    }

    @GetMapping("")
    public ResponseEntity<List<StudentModel>> getAll() {
        return new ResponseEntity<List<StudentModel>>(studentService.getAll(), HttpStatus.OK);
    }
}

pom.xml

<dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.28</version>
        </dependency>


        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.28</version>
        </dependency>


        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.28</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-processing</artifactId>
            <version>2.28</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>2.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.2.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.8</version>
        </dependency>
    </dependencies>

登录TOMCAT服务器

Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.20
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 3 2019 22:26:00 UTC
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.20.0
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_151
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_151-b12
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\core i5\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 9.0
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\core i5\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\core i5\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 9.0\endorsed
Nov 06, 2019 5:25:28 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Nov 06, 2019 5:25:28 PM 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 Files\Java\jre1.8.0_151\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_151/bin/server;C:/Program Files/Java/jre1.8.0_151/bin;C:/Program Files/Java/jre1.8.0_151/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\dev\apache-maven-3.5.4\bin;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\nodejs\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\PuTTY\;C:\LINGO64_18\;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_151\bin;C:\Users\core i5\AppData\Local\Microsoft\WindowsApps;C:\Users\core i5\AppData\Roaming\npm;C:\Users\core i5\AppData\Local\Programs\Microsoft VS Code\bin;;C:\Users\core i5\Desktop\eclipse;;.]
Nov 06, 2019 5:25:28 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Nov 06, 2019 5:25:29 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Nov 06, 2019 5:25:29 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [2,070] milliseconds
Nov 06, 2019 5:25:29 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Nov 06, 2019 5:25:29 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.20]
Nov 06, 2019 5:25:46 PM 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.
Nov 06, 2019 5:26:09 PM 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.
Nov 06, 2019 5:26:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Nov 06, 2019 5:26:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Nov 06, 2019 5:26:09 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [39,962] milliseconds

2 个答案:

答案 0 :(得分:0)

如果您从服务器接收到HTPP状态为404,则表示请求的URI不正确,这意味着未找到此const setCookies = () => { if (Platform.OS === 'android') { CookieManager.setFromResponse( 'https://someweburl.com', `auth-token= xxxxxx`) .then(res => { console.log('CookieManager.setFromResponse =>', res); }) } else { CookieManager.set({ isAndroidApp: true, 'auth-token': '', 'admin-auth-token': '', 'X-CLIENT-NAME': 'SALES_APP', organisationId: 'paramOrganisationId', }).then((res) => { console.log('CookieManager.set =>', res); }); }

强制执行您的要求:

能否请您更新application.properties文件并将此代码添加为以下代码:

'studentsystem2/students'

在您的REST控制器中执行以下操作:

server.servlet.context-path=/studentsystem2
management.server.servlet.context-path = /studentsystem2

答案 1 :(得分:-1)

您要尝试做的是请求映射RequestMapping。 如果要执行GetMapping,则应针对该服务执行GET请求。

 @RequestMapping("")
public ResponseEntity<List<StudentModel>> getAll() {
    return new ResponseEntity<List<StudentModel>>(studentService.getAll(), HttpStatus.OK);
}