有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在名为“DispatcherServlet”的DispatcherServlet中找不到URI为[/user]的HTTP请求的java映射

我正在用Spring boot和Eclipse中的Java8制作一个RESTful服务。每当我试图从邮递员(或任何其他地方)发送HTTP请求时,我都会收到404 Not found代码。在其他方面(代码的变化),我尝试安装Tomcat,但它似乎不是问题(它似乎很好,可以在其他项目上使用)。在过去的两个小时里尝试了我想象中的一切

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 {

}

}

休息控制器: 套餐人力资源。费尔。rznu。控制器

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);
}


}

这是在建的。格雷德尔:

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等。有人有什么建议吗


共 (1) 个答案

  1. # 1 楼答案

    有几件事

    似乎您@SpringBootApplication的包与RestController的包不同

    试着跟着

    @SpringBootApplication(scanBasePackages={"hr.fer.rznu"})
    

    在映射中,删除结尾斜杠/

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    

    @RequestMapping(value = "/user", method = RequestMethod.GET)