RESTful404找不到报错解决方法 - getMessage(), HttpStatus
要解决RESTful404找不到报错,可以尝试以下方法:
http://example.com/api/v1/users/{id}@RequestMapping
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/api/v1/users/{id}")
public User getUser(@PathVariable("id") int id) {
// 实现获取用户的逻辑
}
}
{id}{name}@ExceptionHandler
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
检查缓存问题。如果API接口被缓存,可能会导致找不到报错。可以尝试清除缓存或者使用其他方式访问API接口。,RESTful404找不到报错解决方法 拼多多帮助力入口