【示例】PdServer增加了RESTFULL接口示例

部署运行你感兴趣的模型镜像

欢迎添加微信讨论: cdszsz。或关注公号:AIGC中文站。

PdServer是一个PythonAPI脚手架项目,完成了fastapi,jwt token,mysql,orm,pyqt5,redis等集成,并实现了基于协程的任务队列并发,可以让你脱离基础框架的编码,专注于业务,那就让我们开始吧!

为了方便大家的使用,我们增加了一个接口演示demo,在这个demo里有我们常用的几个接口示例。

项目地址:https://github.com/deanChang2021/PdServer_llm.git

  • PdServer的接口示例

我们准备了一些接口示例在demo_app.py中,全部都已经通过了调试。

/demo/1get 带url参数,一般用于查询详情
/demo?author=zyy&classify=1&pageNum=1&pageSize=5get带url参数,一般用于条件查询list
/demopost通过body传参
/demo/1put用于修改
/uploader/
post上传文件
/demoEvent
geteventStream

详情查询 /demo/1 

图片

列表查询 /demo?author=zyy&classify=1&pageNum=1&pageSize=5

图片

新建 /demo

图片

  • 更新 /demo/1

图片


  • 文件上传 /uploader/

图片


  • eventStream流式 /demoEvent

图片

感谢大家的关注,私信不常看,若有疑问或建议,欢迎加微信: cdszsz。

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

在 Java 中为 RESTful 接口开启登录校验,可从以下几个方面着手: ### 传统方式使用 if - else 语句校验 在早期 Java 项目里,通常采用大量的 if - else 语句对方法入参进行校验。例如在一个登录接口中,对账号和验证码进行非空校验: ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class LoginController { @PostMapping("/loginByVerificationCode/v1") public ResponseResult loginByVerificationCode1(@RequestBody UserLoginParam userLoginParam) { if (userLoginParam.getAccount() == null || userLoginParam.getAccount().isEmpty()) { return RetResponse.makeErrRsp("账号不能为空"); } if (userLoginParam.getVerificationCode() == null || userLoginParam.getVerificationCode().isEmpty()) { return RetResponse.makeErrRsp("验证码不能为空"); } //...其他参数校验,参数越多臃肿 return RetResponse.makeOKRsp(); } } ``` 不过这种方式存在代码冗余、可读性差以及重复劳动等缺点 [^3]。 ### 优化方案:参数优雅校验 可以借助 Java 的 Bean Validation 框架(如 Hibernate Validator)进行参数校验。以下是一个简单示例: ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; @RestController public class LoginController { @PostMapping("/loginByVerificationCode/v2") public ResponseResult loginByVerificationCode2(@Valid @RequestBody UserLoginParam userLoginParam) { return RetResponse.makeOKRsp(); } } ``` 在 `UserLoginParam` 类中添加校验注解: ```java import javax.validation.constraints.NotEmpty; public class UserLoginParam { @NotEmpty(message = "账号不能为空") private String account; @NotEmpty(message = "验证码不能为空") private String verificationCode; // getters and setters public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getVerificationCode() { return verificationCode; } public void setVerificationCode(String verificationCode) { this.verificationCode = verificationCode; } } ``` 通过这种方式,能够减少代码冗余,提升代码的可读性和可维护性 [^3]。 ### 基于 Spring Security 进行登录校验 Spring Security 是一个强大且高度可定制的身份验证和访问控制框架,可用于保护 RESTful 接口。以下是一个简单配置示例: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许公共访问的路径 .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); return http.build(); } } ``` 以上配置可对除 `/public` 路径外的所有请求进行登录校验 [^3]。 ### CAS Server 后台认证 对于复杂定制化登录页的场景,CAS Server 提供了支持 Restful 接口,可用于服务端后台登陆认证。在复杂登陆场景时,可使用接口后台进行认证 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值