我试图通过使用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", "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>
我试图调用UserController.java中的findAll方法,后者又调用USerService.java中的findAll方法。但我认为AppInitializer.java中的getservletMappings方法无效。
帮帮我。
谢谢!
答案 0 :(得分:1)
我在您的代码中看到了几个问题:
您应该像这样更改ComponentScan
注释以明确提及包:
@ComponentScan({"io.egen.controller", "..."})
您的控制器已映射到/users
,但您的AppInitializer
仅配置/api/*
的映射,因此您的控制器将不会被Spring {{1}处理}。