gpt4 book ai didi

java - HTTP 状态 404 与基本 Spring 4 MVC Rest 应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 12:04:42 25 4
gpt4 key购买 nike

我正在尝试遵循基本教程来启动并运行我的应用程序,可以在此处找到该教程:http://websystique.com/springmvc/spring-4-mvc-rest-service-example-using-restcontroller/

目前我有以下类(class):

AppConfig.java:

package com.cgi.miam.config;

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.WebMvcConfigurerAdapter;

@Configuration
@ComponentScan("com.cgi.miam")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {

}

WebAppInitializer.java:

package com.cgi.miam.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}

@Override
protected Class[] getServletConfigClasses() {
return null;
}

@Override
protected String[] getServletMappings() {
return new String[] { "/*" };
}
}

MemberController.java:

package com.cgi.miam.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {

@RequestMapping("/")
public String welcome() {// Welcome page, non-rest
return "Welcome to RestTemplate Example.";
}

@GetMapping("/person/{id}")
public ResponseEntity getCustomer(@PathVariable("id") Integer id) {

return new ResponseEntity("test", HttpStatus.OK);
}
}

还有我的 pom.xml:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cgi.miam</groupId>
<artifactId>miamWebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>miamWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.4</version>
</dependency>

</dependencies>
<build>
<finalName>SpringRest</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>MemberInformation</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>

我已将其部署在 Eclipse 内的 Tomcat 9.0 中,并且启动成功。但是,如果我访问http://localhost:8080/MemberInformation/,我总是会收到404 not found。或http://localhost:8080/MemberInformation/personhttp://localhost:8080/MemberInformation/person/2 。似乎所有的教程都可以在此时启动并获取信息,但这里似乎不起作用。当我浏览到 http://localhost:8080/manager 时,“MemberInformation”应用程序似乎已部署。有什么建议吗?

最佳答案

问题似乎是 Spring 无法找到您的 RestController。在您的 @Configuration 中,请注意您需要如何指定基础包。试试这个:

@ComponentScan(basePackages = "com.cgi.miam") //will look into com.cgi.miam.controller as well

而不是..

@ComponentScan("com.cgi.miam")

关于java - HTTP 状态 404 与基本 Spring 4 MVC Rest 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40516132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com