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

User.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", "[email protected]", "Egen Solutions"));
        users.add(new User(UUID.randomUUID().toString(), "Praveen2", "Salitra", "[email protected]", "Egen Solutions"));
        users.add(new User(UUID.randomUUID().toString(), "Praveen3", "Salitra", "[email protected]", "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方法不起作用。

帮帮我。

谢谢 !

最佳答案

我在您的代码中看到了几个问题:


您可能应该像这样更改ComponentScan批注以明确提及软件包:

@ComponentScan({"io.egen.controller", "..."})
您的控制器已映射到/users,但您的AppInitializer仅为/api/*配置映射,因此Spring的DispatcherServlet不会处理您的控制器。

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

10-09 20:19