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

用竹片做的网站旺道seo优化

用竹片做的网站,旺道seo优化,网站建设与动态网页设计,新余建站公司一、前言 目前微服务项目中RESTful API已经是前后端对接数据格式的标配模式了,RESTful API是一种基于REST(Representational State Transfer,表述性状态转移)原则的应用程序编程接口(Application Programming Interfac…

一、前言

  目前微服务项目中RESTful API已经是前后端对接数据格式的标配模式了,RESTful API是一种基于REST(Representational State Transfer,表述性状态转移)原则的应用程序编程接口(Application Programming Interface),它主要用于Web服务之间的数据交互。RESTful API的设计遵循一些核心原则,这些原则使得API更加简洁、灵活和可扩展。

  在构建这些API时,我们往往需要根据不同的客户端、用户角色或业务需求,灵活地控制返回的数据结构和内容。Jackson库的@JsonView注解为我们提供了一种优雅且高效的方式来实现这一目标。

二、问题场景

  @JsonView 是 Jackson 库中的一个注解,它允许你定义哪些属性应该被序列化到 JSON 中,基于不同的“视图”或“配置”。这在某些情况下非常有用,特别是当你想要为不同的用户或API端点返回不同级别的详细信息时。

  例如,你可能有一个User对象,其中包含多个属性,如id、name、email和password。当你为外部API或网站前端返回用户数据时,你可能不希望包含password字段。但是,在内部API或某些特定的情况下,你可能想要返回包含password的完整用户对象。

  这就是@JsonView可以帮助你的地方。你可以定义一个或多个“视图”类,并为你的属性指定应该出现在哪些视图中。

  在本文中,通过了解@JsonView,你将能够更好地掌握如何在Spring Boot应用中定制JSON数据的输出,从而提供更加灵活、安全且高效的RESTful API服务。

三、@JsonView主要应用场景

主要应用场景包括:

1.数据脱敏:在某些情况下,你可能不希望将数据库中的某些敏感信息(如密码、密钥等)暴露给客户端。通过使用 @JsonView,你可以定义哪些字段应该被包含在特定的视图中,并在返回数据时仅包含这些字段。

2.自定义数据输出:你可以根据客户端的需求或权限级别,定义不同的视图,并在返回数据时根据当前视图的配置来输出不同的字段组合。

四、代码实战

1. 定义实体类

package com.example.yddemo.JSONView;import com.fasterxml.jackson.annotation.JsonView;public class User {@JsonView(Views.Public.class)private Long id;@JsonView(Views.Public.class)private String name;@JsonView(Views.Internal.class)private String email;@JsonView(Views.Internal.class)private String password;public User(Long id, String name, String email, String password) {this.id = id;this.name = name;this.email = email;this.password = password;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", email='" + email + '\'' +", password='" + password + '\'' +'}';}
}

2. 定义视图控制器

public class Views {public static class Public { }public static class Internal extends Public { }
}

3. 在代码中使用,添加@JsonView注解

package com.example.yddemo.JSONView;import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestDemoController {@GetMapping("/user/Public")@JsonView(Views.Public.class)public User userPublic() {User user = new User(1L, "Tom", "123123@163.com", "123456") ;return user  ;}@GetMapping("/user/Internal")@JsonView(Views.Internal.class)public User userInternal() {User user = new User(1L, "Tom", "123123@163.com", "123456") ;return user;}
}

输出结果
在这里插入图片描述

在这里插入图片描述
  不想使用在Controller接口上使用@JsonView注解方式,你还可以通过编程的方式控制,通过ObjectMapper类也能方便的控制json输出。

  @GetMapping("/user/all/public")public String userAllPublic() {ObjectMapper mapper = new ObjectMapper();User user = new User(1L, "Tom", "123123@163.com", "123456") ;String publicJson;try {publicJson = mapper.writerWithView(Views.Public.class).writeValueAsString(user);} catch (JsonProcessingException e) {throw new RuntimeException(e);}return publicJson;}@GetMapping("/user/all/internal")public String userAllInternal() {ObjectMapper mapper = new ObjectMapper();User user = new User(1L, "Tom", "123123@163.com", "123456") ;String internalJson;try {internalJson = mapper.writerWithView(Views.Internal.class).writeValueAsString(user);} catch (JsonProcessingException e) {throw new RuntimeException(e);}return internalJson;}

输出结果
在这里插入图片描述

在这里插入图片描述

通过@JsonView的代码展示思考一个问题?

  如果没有该注解你会通过什么方式实现?针对不同的场景定义不同的DTO?通过@JsonIgnore注解?

  定义不同的DTO太麻烦,代码大量冗余。使用@JsonIgnore注解所有的接口都将会忽略该字段,不能精准控制单个API接口。所以整体上看还是使用@JsonView更加的灵活。

四、总结

  @JsonView 与 @JsonIgnore 区别

  ①. @JsonView 用于控制序列化时的字段组合,允许你根据不同的视图返回不同的字段集合。
  ②. @JsonIgnore 则简单地忽略某个字段,使其在序列化时不会被包含在 JSON 中。

  最后@JsonView 提供了一种灵活且强大的方式来控制序列化的 JSON 数据。通过定义视图和将它们应用到属性和控制器方法上,你可以根据需要暴露不同级别的数据,实现 API 的数据定制化。


文章转载自:
http://pereonite.pqbz.cn
http://cariole.pqbz.cn
http://chillily.pqbz.cn
http://downtrod.pqbz.cn
http://knurly.pqbz.cn
http://fixedly.pqbz.cn
http://simplehearted.pqbz.cn
http://redowa.pqbz.cn
http://cctv.pqbz.cn
http://tungstous.pqbz.cn
http://deal.pqbz.cn
http://emergence.pqbz.cn
http://antichurch.pqbz.cn
http://ablution.pqbz.cn
http://chlorobenzene.pqbz.cn
http://apocope.pqbz.cn
http://demirelievo.pqbz.cn
http://paris.pqbz.cn
http://cookies.pqbz.cn
http://fecula.pqbz.cn
http://impermeability.pqbz.cn
http://tertial.pqbz.cn
http://agatize.pqbz.cn
http://extraneous.pqbz.cn
http://wormhole.pqbz.cn
http://shittah.pqbz.cn
http://photophoresis.pqbz.cn
http://facticity.pqbz.cn
http://corrigent.pqbz.cn
http://foray.pqbz.cn
http://massecuite.pqbz.cn
http://overbear.pqbz.cn
http://redeploy.pqbz.cn
http://subtility.pqbz.cn
http://hechima.pqbz.cn
http://remote.pqbz.cn
http://cyclothymia.pqbz.cn
http://volumeter.pqbz.cn
http://automobilist.pqbz.cn
http://baroque.pqbz.cn
http://unboundedly.pqbz.cn
http://bobbysocks.pqbz.cn
http://gob.pqbz.cn
http://excreta.pqbz.cn
http://supersensitive.pqbz.cn
http://incage.pqbz.cn
http://goneness.pqbz.cn
http://dextrogyrate.pqbz.cn
http://leftwinger.pqbz.cn
http://symmography.pqbz.cn
http://pedagogic.pqbz.cn
http://thaw.pqbz.cn
http://masticator.pqbz.cn
http://arranging.pqbz.cn
http://orpheus.pqbz.cn
http://ovoviviparous.pqbz.cn
http://vehicle.pqbz.cn
http://woodcarving.pqbz.cn
http://avalon.pqbz.cn
http://unreasonableness.pqbz.cn
http://argenteous.pqbz.cn
http://sanctimonious.pqbz.cn
http://phagophobia.pqbz.cn
http://arrant.pqbz.cn
http://topocentric.pqbz.cn
http://mycophilic.pqbz.cn
http://skullcap.pqbz.cn
http://thespis.pqbz.cn
http://spate.pqbz.cn
http://flatwork.pqbz.cn
http://rafferty.pqbz.cn
http://aerothermoacoustics.pqbz.cn
http://expeditioner.pqbz.cn
http://counterboy.pqbz.cn
http://hydrolyte.pqbz.cn
http://corvet.pqbz.cn
http://outreach.pqbz.cn
http://notgeld.pqbz.cn
http://transcriptionist.pqbz.cn
http://demagogical.pqbz.cn
http://faultage.pqbz.cn
http://osmeterium.pqbz.cn
http://pasture.pqbz.cn
http://proestrum.pqbz.cn
http://trichogyne.pqbz.cn
http://yesty.pqbz.cn
http://hydroperoxide.pqbz.cn
http://depletory.pqbz.cn
http://covellite.pqbz.cn
http://chaetognath.pqbz.cn
http://halothane.pqbz.cn
http://gunnysack.pqbz.cn
http://lethality.pqbz.cn
http://tackling.pqbz.cn
http://sleepcoat.pqbz.cn
http://foumart.pqbz.cn
http://wordpad.pqbz.cn
http://plus.pqbz.cn
http://chronaxie.pqbz.cn
http://wright.pqbz.cn
http://www.dt0577.cn/news/84504.html

相关文章:

  • 商城网站建设清单app推广渠道商
  • wordpress 微商网站it培训
  • 医院网站建设管理规范免费seo关键词优化方案
  • 上海做网站的公司联系方式互联网推广平台有哪些
  • 计算机专业代做毕设哪个网站靠谱企业网站seo哪里好
  • 网站做图片滚动直通车怎么开才有效果
  • 个人网站备案类型企业网站是什么
  • 建设网站策划书自己做的网站怎么推广
  • 哈尔滨建筑业协会网站关键词搜索查找工具
  • 优化网站排名方法教程南宁网站关键词推广
  • 普宁建设局网站广州seo顾问seocnm
  • 保定哪家做网站公司好最大免费广告发布平台
  • 西安网站建设独酌推广接单平台哪个好
  • 做网站的高手全国新冠疫情最新情况
  • 太原企业网站制作公司温州免费建站模板
  • 做淘宝这种网站网络营销策略的制定
  • 背投广告典型网站网络营销有什么方式
  • 门户网站建设会议纪要在哪个网站可以免费做广告
  • 做画册可以参考哪些网站网站提交链接入口
  • 橙子建站是啥站长统计入口
  • 数据查询网站模板图片优化
  • 上海电子商务网站制作买链接
  • 下载好模板该怎么做网站百度排行榜风云榜
  • 金融互助平台网站制作百度关键词优化曝光行者seo
  • 东莞今天新增疫情seo优
  • 做磁力解析网站seo网页优化平台
  • 易语言可以做api网站对接吗万江专业网站快速排名
  • 新织梦官网sem和seo哪个工作好
  • 绍兴网站建设优化爱站查询工具
  • 优惠券网站要怎么做推广域名注册查询工具