首页 > 解决方案 > 当我有映射时,出现没有映射的异常

问题描述

我的应用程序运行异常。当我尝试发布到 localhost:8080/addUser 时,我收到没有映射的错误。但是我有它在我的UserController.java

2021-10-03 20:28:04.852  INFO 9896 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server edumeet-shard-00-01.egesp.mongodb.net:27017

com.mongodb.MongoSocketReadException: Prematurely reached end of stream
        at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:131) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:647) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.receiveMessageWithAdditionalTimeout(InternalStreamConnection.java:512) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:355) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:279) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:107) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.2.3.jar:na]
        at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
        at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]

2021-10-03 20:28:05.681  INFO 9896 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-03 20:28:05.692  INFO 9896 --- [  restartedMain] com.example.login.LoginApplication       : Started LoginApplication in 3.95 seconds (JVM running for 4.612)
2021-10-03 20:28:17.960  INFO 9896 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-03 20:28:17.960  INFO 9896 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-10-03 20:28:17.996  INFO 9896 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 36 ms
2021-10-03 20:28:18.025  WARN 9896 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping for POST /addUser
2021-10-03 20:28:18.032  WARN 9896 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound             : No mapping for POST /error

用户控制器.java

package com.example.login.User;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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;

@Controller
@RestController
public class UserController {

    private final UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @RequestMapping(path = "/addUser", method = RequestMethod.POST)
    public String addUser(@RequestBody User user) {
        userRepository.save(user);
        return "added user with id: " + user.getId();
    }
    
    @RequestMapping(path = "/getAllUsers", method = RequestMethod.GET)
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @RequestMapping(path = "/getAllUsers/{id}", method = RequestMethod.GET)
    public Optional<User> getUser(@PathVariable String id) {
        return userRepository.findById(id);
    }
    
    @RequestMapping(path = "/removeUser", method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable String id) {
        userRepository.deleteById(id);
        return "removed user with id" + id;
    }
}

登录应用程序.java

package com.example.login;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;

@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,WebMvcAutoConfiguration.class})
public class LoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginApplication.class, args);
    }

}

标签: javaspringspring-boot

解决方案


您应该将路径更改为值,如下所示:

@RequestMapping(path = "/addUser", method = RequestMethod.POST)
public String addUser(@RequestBody User user) {
    userRepository.save(user);
    return "added user with id: " + user.getId();
}

 change to 

@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@RequestBody User user) {
    userRepository.save(user);
    return "added user with id: " + user.getId();
}

推荐阅读