gpt4 book ai didi

java - Spring 中未调用 Rest Controller

转载 作者:行者123 更新时间:2023-11-30 06:31:14 24 4
gpt4 key购买 nike

我正在使用 Spring Rest Controller 进行 Restful 调用。我有 Spring 4.3.x 版本的 JAR。当我运行项目本身时,index.jsp 没有被调用。我没有在 xml 中配置任何内容,因为我使用的是注释方法。这是我的文件。

P.S:我没有使用 Maven,它是一个动态 Web 项目,所有 Spring JAR(Webmvc、web、core、context、bean)都添加到构建路径中。

我关注了http://viralpatel.net/blogs/spring-4-mvc-rest-example-json/

应用配置

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.ifg.spring")
public class AppConfig {

}

应用程序初始化器

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

}

CustomerDAO

public class CustomerDAO {

// Dummy database. Initialize with some dummy values.
private static List<Customer> customers;
{
customers = new ArrayList();
// Add customers here
}


public List list() {
return customers;
}


}

CustomerRestController

@RestController
public class CustomerRestController {


@Autowired
private CustomerDAO customerDAO;


@GetMapping("/customers")
public List getCustomers() {
return customerDAO.list();
}

}

Web.xml

 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>IFG</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

为什么无法访问 URL http://localhost:8080/IFG/customers ? AppInitializer 文件应该有问题。

任何想法将不胜感激。

最佳答案

您必须在请求映射中指定IFG。根据您的映射,当前链接应为 http://localhost:8080/customers。添加@RequestMapping注解指定路径。

@RestController
@RequestMapping("/IFG")
public class CustomerRestController

关于java - Spring 中未调用 Rest Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46070775/

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