我正在 Eclipse 中使用 Spring boot、Java 8 制作 RESTful 服务。每当我尝试从 Postman(或任何其他地方)发送 HTTP 请求时,我都会收到 404 未找到代码。除其他事项(代码更改)外,我尝试安装 Tomcat 但似乎不是问题(似乎没问题,它适用于其他项目)。在过去的 2 个小时里尝试了我想象中的一切。
package hr.fer.rznu.init;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Lab1Application implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(Lab1Application.class, args);
}
@Override
public void run(String... args) throws Exception {
}
}
休息 Controller : 包 hr.fer.rznu.controller;
import java.util.List;
import hr.fer.rznu.model.User;
import hr.fer.rznu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@RestController
public class RestAPIController {
@Autowired
UserService userService;
@RequestMapping(value = "/user/", method = RequestMethod.POST)
public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
Long id = userService.addUser(user);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(id).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
@RequestMapping(value = "/user/", method = RequestMethod.GET)
public ResponseEntity<List<User>> getListOfUsers() {
List<User> users = userService.getAllUsers();
if(users.isEmpty()){
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
}
这是在 build.gradle 中:
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'lab1'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
如果需要,我可以提供更多代码或信息。我有用户模型和“UserService”方法,例如 getUserById 等。有人有任何建议吗?
最佳答案
有几件事
看来你@SpringBootApplication与RestController在不同的包中
尝试以下,
@SpringBootApplication(scanBasePackages={"hr.fer.rznu"})
在映射中删除结尾斜杠/
@RequestMapping(value = "/user", method = RequestMethod.POST)
和
@RequestMapping(value = "/user", method = RequestMethod.GET)
关于java - 在 DispatcherServlet 中未找到名称为 'dispatcherServlet' 的 URI [/user] 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41058074/