02.response

本文介绍如何在Spring Boot应用中,通过定制ResultCode和Result类,使用链式编程的方式处理Controller返回的List<User>,以便于前端高效绑定。重点讲解了自定义错误状态码和信息,以及如何组织返回结果数据结构。

02.response

控制器中需要对List 返回的结果进行一个处理,才能与前端进行合理的绑定

CustomizeResultCode
package com.zhy.response;

public interface CustomizeResultCode {
    /**
     * 获取错误状态码
     * @return 错误状态码
     */
    Integer getCode();
    /**
     * 获取错误信息
     * @return 错误信息
     */
    String getMessage();
}
ResultCode
package com.zhy.response;

/**
 * @Author: zhy
 * @Description: 返回码定义
 */
public enum ResultCode implements CustomizeResultCode {
    //4.定义方法,枚举的形式
    /**
     * 20000:"成功"
     */
    SUCCESS(20000, "成功"),
    /**
     * 20001:"失败"
     */
    ERROR(20001, "失败");

    //2.定义属性
    private Integer code;
    private String message;

    //3.构造方法
    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    //1.添加对应接口的方法
    @Override
    public Integer getCode() {
        return code;
    }

    //1.添加对应接口的方法
    @Override
    public String getMessage() {
        return message;
    }
}

注解无法使用,将xinguan-base-web的pom的dependencies内容放在xinguan-base-common中

然后让xinguan-base-web得到xinguan-base-common内容

xinguan-base-web的pom.xml

    <dependencies>
        <dependency>
            <groupId>com.zhy</groupId>
            <artifactId>xinguan-base-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
Result
package com.zhy.response;

/**
 * 公共返回结果
 */
@Data
public class Result {
    //1.出入的参数对象
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data = new HashMap<>();

    //2.构造方法私有化,里面的方法都是静态方法
    //达到保护属性的作用
    private Result(){

    }

    //3.这里是使用链式编程
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }
    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(ResultCode.ERROR.getMessage());
        return result;
    }

    //4.自定义返回成功与否
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    //4.自定义返回消息结果
    public Result message(String message){
        this.setMessage(message);
        return this;
    }
    //4.自定义返回(状态码)结果
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
    //4.自定义返回键值对,返回数据库数据
    //Map.put () 方法将获取 Map 集合的所有键名
    public Result data(String key,Object value){
        this.data.put(key,value);
        return this;
    }
    //set方法设置key所对应的键值,然后返回整个Map结构
    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;
    }
}
UserController
package com.zhy.system.controller;
/**
 * 用户表 前端控制器
 * @author zhy
 */
@RestController
@RequestMapping("/system/user")
@Api(value = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;
//    @RequestMapping("/findUsers")     //做查询权限还是要@RequestMapping
    @GetMapping //查询所有
    @ApiOperation(value = "查询所有的用户信息",notes = "")
    /*
     * 0101编写的遍历
     *     public List<User> findUsers(){
     *         List<User> list = userService.list();
     *         return list;
     *    }
     */
    /**
     *  0102编写的遍历
     *  需求修改:控制器中需要对List<User> 返回的结果进行一个处理,才能与前端进行合理的绑定
     *  编写思路:使用链式编程的方法
     */
        public Result findUsers(){
        List<User> list = userService.list();
        return Result.ok().data("users",list);
    }
}
使用vscode连接linux使用python,语句不高亮python连接不上2025-08-22 10:11:02.131 [info] [Error - 10:11:02] Server process exited with code 0. 2025-08-22 10:11:02.132 [info] [Error - 10:11:02] Server initialization failed. 2025-08-22 10:11:02.132 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.132 [info] [Info - 10:11:02] Connection to server got closed. Server will restart. 2025-08-22 10:11:02.132 [info] true 2025-08-22 10:11:02.132 [info] [Error - 10:11:02] Pylance client: couldn't create connection to server. 2025-08-22 10:11:02.132 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.186 [info] [Error - 10:11:02] Server process exited with code 0. 2025-08-22 10:11:02.187 [info] [Error - 10:11:02] Server initialization failed. 2025-08-22 10:11:02.187 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.187 [info] [Info - 10:11:02] Connection to server got closed. Server will restart. 2025-08-22 10:11:02.187 [info] true 2025-08-22 10:11:02.187 [info] [Error - 10:11:02] Pylance client: couldn't create connection to server. 2025-08-22 10:11:02.187 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.252 [info] [Error - 10:11:02] Server process exited with code 0. 2025-08-22 10:11:02.253 [info] [Error - 10:11:02] Server initialization failed. 2025-08-22 10:11:02.253 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.253 [info] [Info - 10:11:02] Connection to server got closed. Server will restart. 2025-08-22 10:11:02.253 [info] true 2025-08-22 10:11:02.253 [info] [Error - 10:11:02] Pylance client: couldn't create connection to server. 2025-08-22 10:11:02.253 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.253 [info] [Error - 10:11:02] Restarting server failed 2025-08-22 10:11:02.253 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.295 [info] [Error - 10:11:02] Server process exited with code 0. 2025-08-22 10:11:02.296 [info] [Error - 10:11:02] Server initialization failed. 2025-08-22 10:11:02.296 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.296 [info] [Info - 10:11:02] Connection to server got closed. Server will restart. 2025-08-22 10:11:02.296 [info] true 2025-08-22 10:11:02.296 [info] [Error - 10:11:02] Pylance client: couldn't create connection to server. 2025-08-22 10:11:02.296 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.296 [info] [Error - 10:11:02] Restarting server failed 2025-08-22 10:11:02.296 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.335 [info] [Error - 10:11:02] Server process exited with code 0. 2025-08-22 10:11:02.336 [info] [Error - 10:11:02] Server initialization failed. 2025-08-22 10:11:02.336 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.336 [info] [Error - 10:11:02] The Pylance server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information. 2025-08-22 10:11:02.336 [info] [Error - 10:11:02] Pylance client: couldn't create connection to server. 2025-08-22 10:11:02.336 [info] Message: Pending response rejected since connection got disposed Code: -32097 2025-08-22 10:11:02.336 [info] [Error - 10:11:02] Restarting server failed 2025-08-22 10:11:02.336 [info] Message: Pending response rejected since connection got disposed Code: -32097
最新发布
08-23
<think>我们正在解决VSCode远程连接Linux时Pylance语言服务无法启动的问题,错误信息为“connection got disposed”。 首先,我们需要明确问题:在远程SSH连接到Linux服务器后,使用VSCode的Python扩展(特别是Pylance)时出现连接错误,导致语法高亮、智能提示等功能失效。 可能的原因和解决方案: 1. **网络连接问题**:虽然SSH连接正常,但扩展主机与远程服务器之间的通信可能受阻。建议检查网络稳定性,尝试重新连接。 2. **扩展版本不兼容**:VSCode本地和远程的扩展版本不一致,或者Pylance扩展本身存在bug。 3. **Python环境问题**:可能远程服务器上的Python解释器路径设置不正确,或者Pylance无法访问该Python环境。 4. **权限问题**:VSCode服务器在远程机器上的安装目录或缓存目录可能没有正确的权限。 5. **缓存损坏**:VSCode在远程服务器上的缓存文件损坏。 6. **防火墙或安全组设置**:某些端口可能被阻止。 7. **资源不足**:远程服务器资源(内存、CPU)不足,导致语言服务启动失败。 具体解决步骤: **步骤1:更新VSCode和所有扩展** - 确保本地VSCode是最新版本。 - 更新所有扩展,特别是Python扩展和Pylance。 - 在远程服务器上,VSCode也会安装一套扩展,需要更新。可以通过VSCode的命令面板(Ctrl+Shift+P)运行`Remote-SSH: Rebuild and Reinstall in Remote`或尝试重新安装远程扩展。 **步骤2:检查Python解释器设置** - 在远程连接后,确保VSCode右下角选择了正确的Python解释器(可能是虚拟环境或系统Python)。 - 可以尝试重新选择解释器:点击右下角的Python版本,选择正确的解释器路径。 **步骤3:清理VSCode远程服务器的缓存** - 远程服务器的VSCode缓存位于`~/.vscode-server`目录(或者如果使用vscode-server-insiders则是`~/.vscode-server-insiders`)。 - 尝试删除该目录(或重命名备份)然后重新连接,VSCode会自动重新安装远程服务器组件。 ```bash rm -rf ~/.vscode-server ``` 或者 ```bash mv ~/.vscode-server ~/.vscode-server.bak ``` 然后重新连接。 **步骤4:检查权限** - 确保你的用户对远程服务器上的相关目录(包括项目目录、Python环境目录、VSCode的目录)有读写权限。 **步骤5:检查资源使用情况** - 使用`top`或`htop`命令查看远程服务器的资源使用情况。如果内存不足,Pylance可能无法启动。 **步骤6:禁用其他扩展** - 有时其他扩展会与Pylance冲突。尝试禁用除Python扩展外的所有扩展,然后逐步启用,找出冲突的扩展。 **步骤7:查看日志** - 在VSCode中打开输出面板(View -> Output),选择“Python”和“Python Language Server”查看详细错误日志。 - 也可以查看远程服务器的日志:在VSCode的命令面板运行`Remote-SSH: Show Log`,选择相应的日志文件。 **步骤8:重新安装Python扩展** - 在扩展面板,卸载Python扩展,然后重新安装。 **步骤9:使用旧版语言服务器** - 如果Pylance问题持续,可以暂时切换回Jedi语言服务器(在设置中搜索`python.languageServer`,设置为`Jedi`)作为临时解决方案。 如果以上步骤仍不能解决问题,可以考虑在VSCode的GitHub仓库或Python扩展的仓库中搜索类似问题,或者提交新的issue。 注意:在操作过程中,请确保备份重要数据。 相关问题: 1. 如何查看VSCode远程服务器的日志? 2. 如何切换Python语言服务器? 3. 如何配置VSCode的远程SSH设置以优化连接?</think>### VSCode 远程连接时 Pylance 初始化失败的解决方案 当 VSCode 通过 Remote-SSH 连接 Linux 服务器时出现 `Pylance server failed to initialize. Connection got disposed` 错误,通常由以下原因导致。请按顺序尝试以下解决方案: --- #### 1. **更新软件和扩展** - **更新 VSCode**:确保本地和远程的 VSCode 均为[最新版本](https://code.visualstudio.com/updates)。 - **更新扩展**: 1. 在远程连接状态下,打开扩展面板(`Ctrl+Shift+X`) 2. 更新 **Python** 和 **Pylance** 扩展 3. 重启 VSCode --- #### 2. **清理远程缓存** **在 Linux 服务器终端执行**: ```bash rm -rf ~/.vscode-server # 清理 VSCode 远程缓存 rm -rf ~/.cache/pylance # 清理 Pylance 缓存 ``` 重新连接后,VSCode 会自动重建缓存[^1]。 --- #### 3. **检查 Python 解释器路径** - 按 `Ctrl+Shift+P` → 输入 `Python: Select Interpreter` - 确保选择的 Python 路径有效(例如 `/usr/bin/python3`) - 若路径无效,手动添加至 `settings.json`: ```json "python.defaultInterpreterPath": "/usr/bin/python3" ``` --- #### 4. **验证语言服务器设置** 在远程的 `settings.json` 中添加: ```json "python.languageServer": "Pylance", "python.analysis.diagnosticMode": "workspace" ``` --- #### 5. **端口和权限检查** - **防火墙**:确保服务器开放端口范围 **50000-65535**(VSCode 远程通信端口): ```bash sudo ufw allow 50000:65535/tcp ``` - **文件权限**:确保项目目录有读写权限: ```bash sudo chown -R $USER:$USER /path/to/project ``` --- #### 6. **降级扩展(临时方案)** 若最新版异常,安装旧版 Pylance: 1. 访问 [Pylance Release 页面](https://github.com/microsoft/pylance-release/releases) 2. 下载 `.vsix` 文件(如 `pylance-2023.1.0.vsix`) 3. 在 VSCode 扩展面板 → 选择「Install from VSIX」 --- #### 7. **查看日志定位问题** - 打开 VSCode 输出面板(`Ctrl+Shift+U`) - 选择 **Python** 和 **Remote-SSH** 日志 - 常见错误线索: - `Permission denied` → 权限问题 - `ECONNRESET` → 网络不稳定 - `Python interpreter not found` → 解释器路径错误 --- ### 相关问题 1. 如何修复 VSCode 远程开发时出现的 `Failed to install VS Code Server` 错误? 2. 在 Linux 服务器上配置 Python 开发环境的最佳实践是什么? 3. VSCode 远程开发中如何优化大型项目的 Pylance 性能? [^1]: 清理缓存会强制 VSCode 重新下载远程组件,通常可解决版本冲突问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值