0
我正在做一个RESTful服务和春天开机,爪哇8在Eclipse中。每当我尝试从Postman(或任何其他地方)发送HTTP请求时,我都会收到404找不到代码。除了其他事情(更改代码)之外,我尝试安装Tomcat,但它似乎不是问题(它似乎很好,它适用于其他项目)。在过去的2个小时里尝试了我想象中的一切。发现HTTP请求的URI与[/ USER]在DispatcherServlet的与名没有映射“的DispatcherServlet”
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')
}
我可以提供更多的代码或信息,如果它的需要。我有模型的用户和“UserService”方法,如getUserById等有没有人有任何建议?
什么是您使用的是邮递员的网址是什么? – Avinash
用调试器检查方法是否被触发?如果没有触发,则可能是您的Url呼叫方式有问题。 –
我使用本地主机:8080 它示出了该线在控制台: 2016年12月9日11:00:10.424 WARN 11308 --- [NIO-8080-EXEC-1] osweb.servlet.PageNotFound:无映射在DispatcherServlet的发现HTTP请求的URI与[/ USER]名为“的DispatcherServlet” 显然收到请求时,它只是/用户未映射某些原因 – pesoodeen