【发布时间】:2016-12-09 10:17:54
【问题描述】:
我正在使用 Spring 启动、Eclipse 中的 Java 8 制作一个 RESTful 服务。每当我尝试从 Postman(或任何其他地方)发送 HTTP 请求时,我都会收到 404 Not found 代码。除其他外(代码更改),我尝试安装 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 {
}
}
休息控制器: 包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')
}
如果需要,我可以提供更多代码或信息。我有用户模型,以及 getUserById 等“UserService”方法。有人有什么建议吗?
【问题讨论】:
-
你在邮递员中使用的网址是什么?
-
用调试器检查方法是否被触发?如果未触发,则您的 Url 调用方式可能存在问题。
-
我正在使用 localhost:8080 它在控制台中显示这一行:2016-12-09 11:00:10.424 WARN 11308 --- [nio-8080-exec-1] o.s.web.servlet .PageNotFound :在 DispatcherServlet 中找不到带有 URI [/user] 的 HTTP 请求的映射,名称为“dispatcherServlet”,显然它接收到请求,只是 /user 由于某种原因没有映射
-
您的邮递员详细信息是什么?你也有堆栈跟踪吗?
-
你试过的网址是什么?
标签: java eclipse spring rest spring-mvc