swagger是如何在java后端工作的



我是一名前端开发人员。后端开发人员离职了。因此,我也将在后端部分工作。我没有任何后端开发的经验。我正在分析后端代码。我没有什么问题。我想澄清一下我的概念。

我已经附上了Java代码文件。

1-这些进口产品是干什么的?:

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

2-为什么我们在声明每个变量之前都放@JsonProperty

3-为什么我们把类名放在几个方法中?例如:

public LedgerAccountRequestDto taxRateId(String taxRateId) {
this.taxRateId = taxRateId;
return this;
}

4-有什么用:

@ApiModel(description = "transaction request")
@Validated**

5-有什么用:

@ApiModelProperty(required = true, value = "")
@NotNull

6-hashCode()方法实际在做什么

public int hashCode() {
return Objects.hash(name, number, typeId, taxRateId);
}

请帮助我理解这些概念。非常感谢

package com.kin.account.api.ledgerAccount.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
import java.util.Objects;
/**
* transaction request
*/
@ApiModel(description = "transaction request")
@Validated
public class LedgerAccountRequestDto   {
@JsonProperty("name")
private String name = null;
@JsonProperty("number")
private String number = null;
@JsonProperty("typeId")
private String typeId = null;
@JsonProperty("taxRateId")
private String taxRateId = null;
public LedgerAccountRequestDto name(String name) {
this.name = name;
return this;
}
/**
* Get name
* @return name
**/
@ApiModelProperty(required = true, value = "")
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LedgerAccountRequestDto number(String number) {
this.number = number;
return this;
}
/**
* Get number
* @return number
**/
@ApiModelProperty(required = true, value = "")
@NotNull
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public LedgerAccountRequestDto typeId(String typeId) {
this.typeId = typeId;
return this;
}
/**
* Get typeId
* @return typeId
**/
@ApiModelProperty(required = true, value = "")
@NotNull
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public LedgerAccountRequestDto taxRateId(String taxRateId) {
this.taxRateId = taxRateId;
return this;
}
/**
* Get taxRateId
* @return taxRateId
**/
@ApiModelProperty(required = true, value = "")
@NotNull
public String getTaxRateId() {
return taxRateId;
}
public void setTaxRateId(String taxRateId) {
this.taxRateId = taxRateId;
}

@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LedgerAccountRequestDto ledgerAccountRequestDto = (LedgerAccountRequestDto) o;
return Objects.equals(this.name, ledgerAccountRequestDto.name) &&
Objects.equals(this.number, ledgerAccountRequestDto.number) &&
Objects.equals(this.typeId, ledgerAccountRequestDto.typeId) &&
Objects.equals(this.taxRateId, ledgerAccountRequestDto.taxRateId);
}
@Override
public int hashCode() {
return Objects.hash(name, number, typeId, taxRateId);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class LedgerAccountRequestDto {n");

sb.append("    name: ").append(toIndentedString(name)).append("n");
sb.append("    number: ").append(toIndentedString(number)).append("n");
sb.append("    typeId: ").append(toIndentedString(typeId)).append("n");
sb.append("    taxRateId: ").append(toIndentedString(taxRateId)).append("n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("n", "n    ");
}
}

我会努力让这件事变得非常简单,并带你一步一步地

1.这些imports是干什么的?:

import com.fasterxml.jackson.annotation.JsonProperty;

要将java包导入到类中,我们需要使用java import关键字,该关键字用于将包及其类访问到java程序中。使用import将内置和用户定义的包访问到java源文件中,这样您的类就可以直接使用其名称引用另一个包中的类。因此,使用上面的示例import com.fasterxml.jackson.annotation.JsonProperty,该行从jackson库导入JsonProperty注释。

2为什么在声明每个变量之前使用@JsonProperty

@JsonProperty

@JsonProperty注释用于在序列化和反序列化过程中使用JSON键映射属性名称。默认情况下,如果您尝试序列化POJO,生成的JSON将具有映射到POJO字段的键。如果要覆盖此行为,可以在字段上使用@JsonProperty注释。它采用一个String属性,该属性指定在序列化期间应映射到字段的名称。

3为什么我们在方法中使用类名?例如:

public LedgerAccountRequestDto taxRateId(String taxRateId) {
this.taxRateId = taxRateId;
return this;
}

上面是一个setter方法,称为narrated,类型为LedgerAccountRequestDto。这与使用String类型的类似示例是一样的。在上面的代码中,该方法将类实例作为返回类型返回。

4以下内容的用途:

@ApiModel(description = "transaction request")
@Validated**

@ApiModel是一个Swagger注释。

Swagger是记录标准API的标准方式。Swagger在azure中部署API时非常有用。Swagger主要用于记录API。为了使其他开发者能够使用API,API必须有适当的文档;否则,他们怎么知道api公开的端点是什么,以及这些端点上支持的操作是什么?他们应该通过什么参数,他们会得到什么?要使用什么身份验证方法?。为了回答这些问题,记录API是非常重要的;如果您希望使用和正确使用API。要了解更多关于Swagger的信息,请查看Swagger-Javatpoint和Swagger–Github repo

  1. @ApiModel-提供有关Swagger型号的其他信息。Swagger-在整个API内省过程中,基于对模型定义的引用重新构建模型定义。@ApiModel允许您操作模型的元数据,从简单的描述或名称更改到多态性的定义。

  2. @Valid@Validated注释-在Spring中,我们使用JSR-303的@Valid注释进行方法级验证。我们还使用它来标记成员属性以进行验证。但是,此注释不支持group validation

    组有助于限制验证过程中应用的约束。一个特殊的用例是UI向导。在第一步中,我们可能有一个子字段组。在接下来的步骤中,可能有另一个组属于同一个bean。因此,我们需要在每个步骤中对这些有限的字段应用约束,但@Valid不支持这一点。

  • 在这种情况下,对于组级别,我们必须使用Spring的@Validated,它是JSR-303's @Valid的变体。这是在方法级别使用的。对于标记成员属性,我们继续使用@Valid注释

5以下各项的用途是什么:

@ApiModelProperty(required = true, value = "")
@NotNull
  • @ApiModelProperty-在Swagger中,它添加和操作模型属性的数据。@ApiModelProperty允许控制特定于Swagger的定义,如允许的值和其他注释。它还提供了额外的过滤属性,以防您在某些情况下想要隐藏该属性
  • required参数指定是否需要该参数。value参数定义了此属性的简要描述

  • @NonNull是一个常见的Spring注释,用于声明带注释的元素不能为null。它表示参数、字段或方法的返回值永远不能为null。这是一个标记注释,没有特定的属性

6hashCode()方法实际在做什么

public int hashCode() {
return Objects.hash(name, number, typeId, taxRateId);
}

hashCode方法是一个内置的方法,它返回输入值的整数散列值。要使用示例正确理解hashCode()equals(),请查看Java-scaler.com中的HashCode(),以及Java中的HashCode方法是什么?-教育性.io

忠告

如果你没有基础知识,你就必须学习Java,以这种方式进行研究将非常困难。查看以下资源链接以学习Java和Spring引导。

学习Java

  1. 笔记-学习Java-作者Jakob Jenkov
  2. 视频-学习Java-通过Java指南

学习Spring Boot

  1. Notes-学习SpringBoot-通过Javatpoint
  2. 视频-学习SpringBoot-通过Java指南

相关内容

最新更新