代码之家  ›  专栏  ›  技术社区  ›  Rafał Sokalski

invoke rest controller方法返回404 NULL

  •  0
  • Rafał Sokalski  · 技术社区  · 6 年前

    我想创建雾建筑。我在云和客户机服务器应用程序中有两个服务器,其中服务器类似于雾服务器。云服务器用于与数据库通信。问题是当我尝试在我的应用程序中注册时。我想检查验证时刻数据库中是否存在用户。当我调用rest方法时,它返回“404 NULL”。

    使用方法来检查用户是否存在的REST控制器。当我使用 http://localhost:8081/user/test 它返回JSON

    {"userID":1,"username":"test","password":"test","events":[]}
    

    用户控制器.java

        @RestController
        public class UserController {
    
        @Autowired
        private UserRepository userRepository;
    
        @RequestMapping(value = "/user/{username}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<User> getUserByUsername(@PathVariable("username") String username) {
            User user = userRepository.findByUsername(username);
    
            if (user == null) {
                return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
            }
    
            return new ResponseEntity<User>(user, HttpStatus.OK);
        }
    
        @RequestMapping(value = "/user/", method = RequestMethod.POST)
        public ResponseEntity<Void> addUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
            userRepository.save(user);
    
            HttpHeaders headers = new HttpHeaders();
            headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getUserID()).toUri());
            return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
        }
    }
    

    现在我试着让那个用户通过这种方式进入Fog服务器:

    public boolean sendRequestIfUserExists(String username) {
        CloudServer cloudServer = getAvailableServer();
        if (cloudServer != null) {
            ResponseEntity<User> userEntity = restTemplate.getForEntity(cloudServer.getURI() + "/user/" + username, User.class);
            changeServerStatusToAvailable(cloudServer.getServerName());
            HttpStatus status = userEntity.getStatusCode();
            if (status == HttpStatus.NOT_FOUND) {
                SpeechRecognitionApplication.logger.info("User: " + username + "does not exist");
                return false;
            }
        }
    
        return true;
    }
    

    这条线是:

      ResponseEntity<User> userEntity = restTemplate.getForEntity(cloudServer.getURI() + "/user/" + username, User.class);
    

    org.springframework.web.client.HttpClientErrorException: 404 null
        at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:775) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:728) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:684) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:359) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at pl.speechrecognition.services.CloudService.sendRequestIfUserExists(CloudService.java:65) ~[classes/:na]
        at pl.speechrecognition.controller.IndexController.postRegister(IndexController.java:53) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]
    

    编辑: 最后我找到了那个例外的解决方案。这是因为我在用户类中有错误的构造函数。

    用户.java

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class User {
    
    private Long userID;
    private String username;
    
    private String password;
    private List<Event> events;
    
    public Long getUserID() {
        return userID;
    }
    
    public void setUserID(Long userID) {
        this.userID = userID;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public List<Event> getEvents() {
        return events;
    }
    
    public void setEvents(List<Event> events) {
        this.events = events;
    }
    
    public User(String username) {
        this.username = username;
    }
    
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public String toString() {
        return "Username: " + username;
    }
    
    
    // Missing constructor
    public User(Long userID, String username, String password, List<Event> events) {
        this.userID = userID;
        this.username = username;
        this.password = password;
        this.events = events;
    }
    }
    

    但现在它抛出:

    org.springframework.web.client.RestClientException: Error while extracting response for type [class pl.speechrecognition.model.User] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `pl.speechrecognition.model.User` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `pl.speechrecognition.model.User` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
     at [Source: (PushbackInputStream); line: 1, column: 2]
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:115) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1000) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:983) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:730) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Rafał Sokalski    6 年前

    最后我找到了解决办法。构造函数有问题。当我用所有参数添加构造函数时,我发现反序列化JSON Spring还需要零参数构造函数的信息。

        2
  •  1
  •   sharpcodes    6 年前

    您是否尝试启用日志以查看呼叫的启动位置? 尝试添加:logging.level.org.springframework.web=debug 您应该看到完整的URL。

    希望这能帮助您进一步调试。