gpt4 book ai didi

java - Servlet 调用失败。使用 Spring 支架

转载 作者:行者123 更新时间:2023-12-01 10:05:09 25 4
gpt4 key购买 nike

我正在尝试使用 requestMapping 注释来调用 servlet。我有一个在服务器上运行的 index.html 页面。但是当我尝试通过键入来调用 servlet 时 http://localhost:8080/spring-rest/api/users它不起作用。我的项目中有 5 个类(class)。这是代码。

用户.java

    package io.egen.entity;

import lombok.Data;

@Data
public class User {


public User(String id, String firstName, String lastName, String email, String compnay) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.compnay = compnay;
}
private String id;
private String firstName;
private String lastName;
private String email;
private String compnay;
}

UserController.java

package io.egen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.egen.entity.User;
import io.egen.services.UserService;

@RestController
@RequestMapping(value="/users")
public class UserController {

@Autowired
private UserService services;

@RequestMapping(method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<User> findAll() {
return services.findAll();
}

}

UserService.java

    package io.egen.services;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import io.egen.entity.User;

@Service
public class UserService {

public List<User> findAll() {
System.out.println("Inside UserService");
List<User> users = new ArrayList<User>();
users.add(new User(UUID.randomUUID().toString(), "Praveen1", "Salitra", "psalitra1@egen.io", "Egen Solutions"));
users.add(new User(UUID.randomUUID().toString(), "Praveen2", "Salitra", "psalitra2@egen.io", "Egen Solutions"));
users.add(new User(UUID.randomUUID().toString(), "Praveen3", "Salitra", "psalitra3@egen.io", "Egen Solutions"));
return users;
}
}

AppInitializer.java

package io.egen;

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

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

@Override
protected String[] getServletMappings() {
System.out.println("HEy");
return new String [] {"/api/*"};
}


}

AppConfig.java

    package io.egen;

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

@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig {

}

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>io.egen</groupId>
<artifactId>spring-rest</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<name>spring-rest Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

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


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<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.7.3</version>
</dependency>

</dependencies>

</project>

Here is my package structure

我试图调用 UserController.java 中的 findAll 方法,该方法又调用 USerService.java 中的 findAll 方法。但我认为AppInitializer.java中的getservletMappings方法不起作用。

帮帮我吧。

谢谢!

最佳答案

我在您的代码中发现了几个问题:

  1. 您可能应该像这样更改 ComponentScan 注释以明确提及包:

    @ComponentScan({"io.egen.controller", "..."})

  2. 您的 Controller 已映射到 /users,但您的 AppInitializer 仅配置 /api/* 的映射,因此您的 Controller 将不由 Spring 的 DispatcherServlet 处理。

关于java - Servlet 调用失败。使用 Spring 支架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534275/

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