简体   繁体   中英

No mapping found for HTTP request with URI [/user] in DispatcherServlet with name 'dispatcherServlet'

I'm making a RESTful service with Spring boot, Java 8 in Eclipse. Whenever I try to send a HTTP request from Postman(or any other place) I get 404 Not found code. Among other things(changes in code), I tried installing Tomcat but it seems not to be the problem(it seems to be fine, it works on other projects). Tried everything in my imagination for the past 2 hours.

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 {

}

}

Rest controller: package 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);
}


}

This is in 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')
}

I can provide more code or info if it's needed. I have model for user, and "UserService" methods like getUserById etc. Does anybody have any suggestions?

Couple of things

seems you @SpringBootApplication in different package than of RestController

Try following,

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

in mapping remove the ending slash /

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

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM