当前位置: 首页 > news >正文

网站用ps下拉效果怎么做的闽候县建设局网站

网站用ps下拉效果怎么做的,闽候县建设局网站,wordpress 首页弹窗,网页设计认证培训Spring Boot 和 ASP.NET Core 都是企业中流行的 Web 框架, 对于喜欢 C# 的人会使用 ASP.NET Core, 而对于 Java 或 Kotlin 等基于 JVM 的语言#xff0c;Spring Boot 是最受欢迎的。这本文中#xff0c;会对比这两个框架在以下方面有何不同#xff1a;•控制器•模型绑定和验… Spring Boot 和 ASP.NET Core 都是企业中流行的 Web 框架, 对于喜欢 C# 的人会使用 ASP.NET Core, 而对于 Java 或 Kotlin 等基于 JVM 的语言Spring Boot 是最受欢迎的。这本文中会对比这两个框架在以下方面有何不同•控制器•模型绑定和验证•异常处理•数据访问•依赖注入•认证与授权•性能基础项目这是一个有关订单的基础项目, 非常简单的后端 api, 客户可以创建一个订单来购买一个或多个产品, 我使用了 MySQL 作为数据库下面是实体关系图。这里使用的框架版本分别是, Spring Boot (v2.5.5) 和 .NET 6, 让我们开始对比吧1.控制器控制器是负责处理传入请求的层, 为了在 Spring Boot 中定义一个控制器我创建了一个类 ProductOrderController, 然后使用了 RestController 和 RequestMapping 注解, 然后在控制器的每个方法上, 可以使用下面的注解来定义支持的 HTTP 方法和路径可选。•GetMapping•PostMapping•PutMapping•DeleteMapping•PatchMapping如果要绑定到路径变量, 我们可以将参数添加到用PathVariable 注释的控制器方法中并指定与参数同名的路由路径模板下面的 getOrderById() 方法我们将id绑定为路径变量。RestController RequestMapping(/v1/orders) class ProductOrderController(private val productOrderService: IProductOrderService ) {GetMappingfun getOrders(query: ProductOrderQuery): ListProductOrderDto when {query.productId?.isNotEmpty() true - productOrderService.getByProductId(query.productId!!)query.customerId?.isNotEmpty() true - productOrderService.getByCustomerId(query.customerId!!)else - productOrderService.getAllOrders()}GetMapping({id})fun getOrderById(PathVariable id: String): ProductOrderDto productOrderService.getById(id) }在 .NET Core 中, 控制器和上面是相似的, 首先创建一个 ProductOrderController类, 并继承 ControllerBase 标记 [ApiController] 特性, 然后通过 [Route] 特性指定基本路径, 然后在控制器的每个方法上, 可以使用下面的特性来定义支持的 HTTP 方法和路径可选。[ApiController] [Route(v1/orders)] public class ProductOrderController : ControllerBase {private readonly IProductOrderService _productOrderService;public ProductOrderController(IProductOrderService productOrderService){_productOrderService productOrderService;}[HttpGet]public async TaskListProductOrderDto GetOrders([FromQuery] ProductOrderQuery query){ListProductOrderDto orders;if (!string.IsNullOrEmpty(query.ProductId)){orders await _productOrderService.GetAllByProductId(query.ProductId);}else if (!string.IsNullOrEmpty(query.CustomerId)){orders await _productOrderService.GetAllByCustomerId(query.CustomerId);}else{orders await _productOrderService.GetAll();}return orders;}[HttpGet({id})]public async TaskProductOrderDto GetOrderById(string id) await _productOrderService.GetById(id); }2.模型绑定和验证在 Spring Boot 中, 我们只需要给控制器的方法的参数加上下面的注解•RequestParam → 从查询字符串绑定•RequestBody → 从请求体绑定•RequestHeader → 从请求头绑定对比表单的请求不需要给参数加注解就可以绑定。RestController RequestMapping(/v1/customer) class CustomerController(private val customerService: CustomerService ) {PostMapping(/register)fun register(Valid RequestBody form: RegisterForm) customerService.register(form)PostMapping(/login)fun login(Valid RequestBody form: LoginForm) customerService.login(form) }RestController RequestMapping(/v1/orders) class ProductOrderController(private val productOrderService: IProductOrderService ) {GetMappingfun getOrders(query: ProductOrderQuery): ListProductOrderDto {...} }如果要对参数进行验证, 需要添加 spring-boot-starter-validation 依赖项, 然后给 DTO 的属性加上 NotEmpty、Length 等注解 最后给DTO加上 Valid 即可。.NET Core 和上面类似, 同样你可以使用下面的特性标记控制器的方法•[FromQuery] → 从查询字符串绑定•[FromRoute] → 从路由数据绑定•[FromForm] → 从表单数据绑定•[FromBody] → 从请求体绑定•[FromHeader] → 从请求头绑定[Route(v1/customer)][ApiController]public class CustomerController : ControllerBase{[HttpPost(register)]public async TaskAuthResultDto Register([FromBody] RegisterForm form) await _customerService.Register(form);[HttpPost(login)]public async TaskAuthResultDto Login([FromBody] LoginForm form) await _customerService.Login(form);}[Route(v1/orders)][ApiController]public class ProductOrderController : ControllerBase{[HttpGet]public async TaskListProductOrderDto GetOrders([FromQuery] ProductOrderQuery query){.....}}模型验证也是类似的, 给 DTO 的属性上加上 [Required]、[MinLength]、[MaxLength] 等特性就可以了。public class RegisterForm {[Required(ErrorMessage Please enter user id)]public string UserId { get; set; }[Required(ErrorMessage Please enter name)]public string Name { get; set; }[Required(ErrorMessage Please enter password)][MinLength(6, ErrorMessage Password must have minimum of 6 characters)]public string Password { get; set; } }3.异常处理Spring Boot 的异常处理主要用 RestControllerAdvice 和 ExceptionHandler注解如下abstract class AppException(message: String) : RuntimeException(message) {abstract fun getResponse(): ResponseEntityBaseResponseDto }RestControllerAdvice class ControllerExceptionHandler : ResponseEntityExceptionHandler() {ExceptionHandler(AppException::class)fun handleAppException(ex: AppException, handlerMethod: HandlerMethod): ResponseEntityBaseResponseDto {return ex.getResponse()} }在 ASP.NET Core 中异常处理程序被注册为过滤器/中间件我们可以创建一个异常处理类并继承 IExceptionFilter 接口。public class ControllerExceptionFilter : IExceptionFilter {public void OnException(ExceptionContext context){if (context.Exception is AppException exception){context.Result exception.GetResponse();}} }然后注册这个异常过滤器var builder WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddControllers(options {options.Filters.AddControllerExceptionFilter(); });4.数据访问在 Spring Boot 中 你可以使用 Hibernate ORM, 创建一个Repository 接口, 并继承 JpaRepository , 这样就有了开箱即用的基本查询方法比如 findAll() 和 findById()。您还可以在定义自定义查询方法。只要遵循严格的方法命名约定Spring 就会构建这个存储库的实现包括运行时的所有查询魔法是的interface IProductOrderRepository : JpaRepositoryProductOrder, String {EntityGraph(type EntityGraph.EntityGraphType.FETCH, value product-order-graph)override fun findById(id: String): OptionalProductOrderEntityGraph(type EntityGraph.EntityGraphType.FETCH, value product-order-graph)fun findAllByCustomer(customer: Customer): ListProductOrderEntityGraph(type EntityGraph.EntityGraphType.FETCH, value product-order-graph)Query(SELECT ord FROM ProductOrder ord JOIN OrderItem item ON item.productOrder ord WHERE item.productId :productId)fun findAllByProductId(productId: String): ListProductOrder }而在 .NET Core 中我们可以使用官方的 Entity Framework ORM, 首先我们需要创建一个 DB Context 类, 这是 ORM 框架用来连接数据库和运行查询的桥梁。public class AppDbContext : DbContext {public DbSetCustomer Customer { get; set; }public DbSetProduct Product { get; set; }public DbSetProductOrder ProductOrder { get; set; }public DbSetOrderItem OrderItem { get; set; }public AppDbContext(DbContextOptionsAppDbContext options) : base(options){Customer SetCustomer();Product SetProduct();ProductOrder SetProductOrder();OrderItem SetOrderItem();} }接下来还需要注册上面的 DB Context并配置数据库连接字符串var builder WebApplication.CreateBuilder(args);// Add services to the container. builder.Services.AddDbContextAppDbContext(options {// Using Pomelo.EntityFrameworkCore.MySql libraryoptions.UseMySql(builder.Configuration.GetConnectionString(EaterMysql), ServerVersion.Parse(8.0.21-mysql)); });在我们的 Repository 中我们访问 DB 上下文中的 DbSet 字段来执行查询, 在这里我们使用 LINQ这是一组直接融入 C# 语言的 API用于从各种数据源进行查询。这是我非常喜欢的一项功能因为它提供了 Fluent API例如 Where()、Include() 或 OrderBy()这非常方便public class ProductOrderRepository : BaseRepositoryProductOrder, IProductOrderRepository {public ProductOrderRepository(AppDbContext context) : base(context){}public TaskProductOrder? GetById(string id) _context.ProductOrder.Include(o o.Customer).Include(o o.Items).Where(o o.Id id).FirstOrDefaultAsync();public TaskListProductOrder GetAllByCustomer(Customer customer) _context.ProductOrder.Include(o o.Items).Where(o o.Customer customer).ToListAsync();public TaskListProductOrder GetAllByProductId(string productId) _context.ProductOrder.Include(o o.Customer).Include(o o.Items).Where(o o.Items.Any(item item.ProductId productId)).ToListAsync(); }5.依赖注入Spring Boot 中的依赖注入真的非常简单, 只需根据类的角色使用 Component、**Service 或Repository** 等注解即可在启动时它会进行扫描然后注册。Service class ProductOrderService(private val customerRepository: ICustomerRepository,private val productOrderRepository: IProductOrderRepository,private val mapper: IMapper ) : IProductOrderService {// ...// ...// ... }在 .NET Core 中, 服务根据生命周期分成3中类型单例的范围的, 瞬时的并且在启动时手动注册到 DI 容器中var builder WebApplication.CreateBuilder(args);// Add services to the container.// Services builder.Services.AddSingletonIPasswordEncoder, PasswordEncoder(); builder.Services.AddSingletonITokenService, TokenService(); builder.Services.AddScopedIProductOrderService, ProductOrderService(); builder.Services.AddScopedICustomerService, CustomerService();// Repositories builder.Services.AddScopedIProductOrderRepository, ProductOrderRepository(); builder.Services.AddScopedICustomerRepository, CustomerRepository();6.身份验证和授权在 Spring Boot 中, 首先需要添加依赖 spring-boot-starter-security, 然后在 build.gradle 文件或 pom.xml如果您使用 Maven中为 JWT 库添加以下依赖项implementation(io.jsonwebtoken:jjwt-api:${jjwtVersion}) implementation(io.jsonwebtoken:jjwt-impl:${jjwtVersion}) implementation(io.jsonwebtoken:jjwt-jackson:${jjwtVersion})接下来, 需要创建一个负责 JWT 令牌解析和验证的过滤器/中间件, 然后重写 doFilterInternal 方法, 编写解析和验证逻辑。class JwtAuthenticationFilter(private val tokenService: ITokenService ) : OncePerRequestFilter() {override fun doFilterInternal(request: HttpServletRequest,response: HttpServletResponse,filterChain: FilterChain) {val authorization request.getHeader(Authorization)if (authorization null || !authorization.startsWith(Bearer)) {return filterChain.doFilter(request, response)}val token authorization.replaceFirst(Bearer , )val claims try {tokenService.parse(token).body} catch (ex: JwtException) {SecurityContextHolder.clearContext()return}// Set authentication to tell Spring that the user is valid and authenticated.SecurityContextHolder.getContext().authentication UsernamePasswordAuthenticationToken(claims.id, null, arrayListOf())filterChain.doFilter(request, response)} }要配置和强制执行身份验证需要先创建一个继承WebSecurityConfigurerAdapter的配置类并使用 Configuration 注解, 在这里注册我们上面创建的 JWT 过滤器并在configure方法中配置哪些端点应该进行身份验证。比如我允许匿名访问客户登录和注册端点。其他所有内容都应进行身份验证class ApiAccessDeniedHandler : AccessDeniedHandler {override fun handle(request: HttpServletRequest,response: HttpServletResponse,accessDeniedException: AccessDeniedException) {response.status HttpStatus.FORBIDDEN.value()} }class AuthEntryPoint : AuthenticationEntryPoint {override fun commence(request: HttpServletRequest,response: HttpServletResponse,authException: AuthenticationException) {response.status HttpStatus.UNAUTHORIZED.value()} }Configuration class SecurityConfig(tokenService: ITokenService ) : WebSecurityConfigurerAdapter() {private val jwtAuthenticationFilter JwtAuthenticationFilter(tokenService)Beanfun passwordEncoder(): PasswordEncoder BCryptPasswordEncoder()override fun configure(http: HttpSecurity) {http.csrf().disable().cors().disable().addFilterAfter(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter::class.java).exceptionHandling().accessDeniedHandler(ApiAccessDeniedHandler()).authenticationEntryPoint(AuthEntryPoint()).and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers(/v1/customer/register, /v1/customer/login).permitAll().anyRequest().authenticated()} }在 ASP.NET Core 中实现 JWT 身份验证和授权非常简单 首先安装Microsoft.AspNetCore.Authentication.JwtBearer NuGet 包, 然后在 Program.cs 文件中配置一些设置例如密钥、颁发者和到期时间。var builder WebApplication.CreateBuilder(args);// Configure JWT Authentication builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options {options.SaveToken true;options.RequireHttpsMetadata true;options.TokenValidationParameters new TokenValidationParameters(){ValidateAudience false,ValidIssuer builder.Configuration[JWT:ValidIssuer],IssuerSigningKey new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration[JWT:Secret])),ClockSkew TimeSpan.FromSeconds(30)};});var app builder.Build();// Enable Authentication Authorization app.UseAuthentication(); app.UseAuthorization();app.MapControllers();app.Run();如果需要认证就在控制或者方法上加上 [Authorize] 特性, 同样可以加上 [AllowAnonymous] 代表允许匿名访问。[Route(v1/customer)] [ApiController] [Authorize] public class CustomerController : ControllerBase {[HttpPost(login)][AllowAnonymous]public async TaskAuthResultDto Login([FromBody] LoginForm form) await _customerService.Login(form);[HttpGet]public async TaskCustomerDto GetProfile() await _customerService.GetProfile(); }7.性能最后是关键的部分性能, 这两个框架在 QPS 和 内存使用率方面的表现如何在这里我做了一个负载测试调用一个 API通过 id 获取一个产品订单。  测试环境CPUIntel Core i7–8750H 4.10 GHz6 核 12 线程 RAM32 GB 操作系统Windows 11  测试设置我使用的压力测试工具是 K6, 进行了2次测试, 因为我想看看程序预热后性能提高了多少。在每次测试中前 30 秒将从 0 增加到 1000 个虚拟用户然后在那里停留 1 分钟。然后再过 30 秒测试将从 1000 用户减少到 0 用户。我还将 Golang使用 Gin 框架和 Gorm添加到基准测试, 这里只是为了对比 我们都知道 Golang 非常快。  测试结果显然Golang 是最快的我检查了两者都执行了查询优化确认没有 N1 问题所以在 QPS 上 .NET Core 胜出。在内存使用方面Golang 当然是最小的只有 113 MB其次是 .NET Core 最后就是超过1 GB 内存的 Spring Boot, 另外我观察到的有趣的事情是测试完成后Golang 和 .NET Core 的内存消耗分别减少到 10 MB 和 100 MB 左右而 Spring Boot 保持在 1 GB 以上直到我终止进程。最后Spring Boot 和 ASP.NET Core 都是非常成熟的框架您都可以考虑使用, 希望对您有用
http://wiki.neutronadmin.com/news/181801/

相关文章:

  • 只做网站不推广能行吗制作图片的软件哪个好用
  • 嘉兴网站建设系统建设企业网站方法
  • wordpress给公司建站英德住房和城乡建设局网站
  • 平凉市住房和城乡建设厅网站合肥公司建设网站
  • 思茅北京网站建设wordpress 4.8.3 漏洞
  • ps如何做网站轮播图快速排名新
  • 哈尔滨建站公司可以不使用备案的网站名吗
  • 买卖网站建设怎么免费做公司网站
  • 成华区微信网站建设推广金融网站策划
  • 网站赞赏网站怎么做防360拦截
  • 自媒体交易网站开发wordpress三栏模板下载
  • 英文网站外链查询荣添网站建设优化
  • 用dw做网站用div布局现在室内设计师好做吗
  • 网站可以做动态背景吗网站建设中 页面
  • 企业网站建设的步骤过程部队网站源码
  • 禅城网站建设代理定制v
  • 什么叫做网站整站上海十大科技公司
  • 国内大一html网站简单设计桂林建设银行招聘网站
  • cdr做的网站效果怎么直接用洛阳疾控最新通告今天
  • 做好的网站怎么注销嘉兴手机网站制作
  • 网站开发静态怎样转成动态html5特效网站
  • 英文美食网站怎么做wordpress 4.1 主题
  • 门户网站建设需求文档网站名称与域名
  • 政协网站 两学一做专题研讨wordpress标题颜色
  • 手机app开发上市公司seo网站推广 杭州
  • 重庆快速排名网站的优化方案
  • 二手交易网站开发可参考文献直播网站建设模板
  • erp开发和网站开发模板图片可爱
  • 公司免费网站域名注册建立诊断的步骤
  • 网站建设售后回访话术公司网站后台是什么