簡體   English   中英

調用REST控制器方法返回404 null

[英]Invoke REST controller method returns 404 null

我想創建霧架構。 我在雲和客戶端服務器應用程序中有2台服務器,其中服務器有點像霧服務器。 雲服務器用於與數據庫通信。 問題是當我嘗試在我的申請中進行注冊時。 我想在驗證時刻檢查用戶是否存在於數據庫中。 當我調用rest方法時,它返回“ 404 null”。

REST控制器,帶有檢查用戶是否存在的方法。 當我使用http:// localhost:8081 / user / test調用此方法時,它將返回JSON

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

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

現在,我嘗試通過這種方式使該用戶進入霧服務器:

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]

編輯:最后,我找到了該異常的解決方案。 這是因為我在User類中構造錯誤。

User.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]

終於我找到了解決方案。 構造函數存在問題。 當我向構造函數添加所有參數時,我發現反序列化JSON的信息Spring還需要零參數構造函數

您是否嘗試啟用日志以查看將呼叫確切發起到的位置? 嘗試添加:logging.level.org.springframework.web = DEBUG您應該看到完整的URL。

希望這可以幫助您進一步調試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM