java继承RESTAPI和子对象的使用

eqoofvh9  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(300)

我正在用构建一个restapi resteasy 我在这里申请继承权有些困难。让我来解释一下这是怎么回事。我有一个端点叫做 /parties . 在申请中有两种当事人。个人和组织。这不能更改为使用2个不同的资源,它被设计为使用资源 parties 两个都是。我已经创建了3个模型称为 PartyModel , IndividualModel 以及 OrganizationModel . IndividualModel 以及 OrganizationModel 你的孩子 PartyModel . 同时 PartyModel 是的孩子 IdentifiableEntity . 后者是几乎所有模型都使用的通用分类。
这些是模型。我喜欢能手和能手:
可识别实体

public class IdentifiableEntity   {
  @ApiModelProperty(example = "9eeb19e7-62e7-4fdb-8a99-fb77e51dda0a", value = "The unique, technical identifier")
  @JsonProperty("uuId")
  @Pattern(regexp="[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}")
  private String uuId;

}
派对模型

public class PartyModel extends IdentifiableEntity  {
  @ApiModelProperty(value = "Discriminator for the type of party")
  @JsonProperty("partyType")
  @NotNull
  private PartyTypeEnum partyType;
}

个体模型

public class IndividualModel extends PartyModel {

  @ApiModelProperty(value = "")
  @JsonProperty("individualName")
  private IndividualName individualName;
  @ApiModelProperty(value = "")
  @JsonProperty("company")
  private OrganizationModel company;
}

组织模式

public class OrganizationModel extends PartyModel {

  @ApiModelProperty(example = "VF", value = "")
  @JsonProperty("mnemonic")
  private String mnemonic;
  @ApiModelProperty(example = "Vodafone", value = "A short, commonly used name of the organization")
  @JsonProperty("shortName")
  private String shortName;
  @ApiModelProperty(value = "")
  @JsonProperty("organizationName")
  private OrganizationName organizationName = null;
}

这是我的控制器:
第四部分

@Path("/parties")
@Api(description = "the parties API")
public class PartiesApi  {

  @Inject
  private PartiesApiService service;

  @POST
  @Consumes({ "application/json" })
  @Produces({ "application/json" })
  @ApiOperation(value = "Create Party", notes = "Creates a new party. The uuId may be client provided, otherwise one is generated. If a client provided uuId is already in use, a 409 error code will be returned.", response = Object.class, tags={  })
  @ApiResponses(value = {
        @ApiResponse(code = 200, message = "The created party.", response = Object.class),
        @ApiResponse(code = 400, message = "The request was not understood. Additional information may be available in the included ApiError", response = ApiError.class),
        @ApiResponse(code = 409, message = "There is a conflict with the resource that does not allow the service to process the request as sent. The request may work if repeated later or if the resource is modified beforehand. Additional information may be available in the included ApiError", response = ApiError.class),
        @ApiResponse(code = 500, message = "An internal error occured. The request should not be repeated. Additional information may be available in the included ApiError", response = ApiError.class) })
public Response createParty(@ApiParam(value = "The party to create" ) PartyModel body, @ApiParam(value = "The authorization scheme and credentials. Supported schemes: Bearer (via Keycloak). The parameter is not technically required but all requests without it (exception for OPTIONS requests) will result in a 401 - Unauthorized response" )@HeaderParam("Authorization") String authorization, @Context SecurityContext securityContext)
        throws Exception {
    PartyModel partySaved = service.createParty(body,authorization,securityContext);
    return Response.ok(partySaved).build();
  }
}

这就是服务:
partyserviceimpl公司

@RequestScoped
@Stateful
public class PartiesApiServiceImpl implements PartiesApiService {

  @Inject
  private PartyRepositoryIface partyRepository;

  @Inject
  private PartyMapper partyMapper;

  public PartyModel createParty(PartyModel partyToSave, String authorization, SecurityContext securityContext) throws ApiException {
      //The UUID was not given in the request. Therefore we generate it randomly.
      if(Objects.isNull(partyToSave.getUuId()) || partyToSave.getUuId().isEmpty()){
        partyToSave.setUuId(UUID.randomUUID().toString());
      }
      PartyEntity partyEntityToSave = partyMapper.dtoToEntity(partyToSave);
      //An exception will be thrown in case the party to save already exists. Based on the UUID
      Optional<PartyEntity> partyEntitySaved = partyRepository.save(partyRepository.findById(partyEntityToSave.getUuid()).orElseThrow(() -> new ApiException(HttpStatus.SC_CONFLICT, "This party already exists")));
      return partyMapper.entityToDto(partyEntitySaved.get());
    }
  }

因此,我的问题来时,我试图发送一个邀请作为请求。我需要进入它的财产 private OrganizationModel company 为了让它有逻辑性。但既然我路过一个 PartyModel 作为参数,我无法直接访问。我宁愿避免做一个对象或使用铸造 instanceof .
我在这里和互联网上查看了许多与restapi继承相关的主题,但没有一个能澄清我的问题。
因此,我如何处理这件事?我如何使用 Party 作为请求的父类,但能够对模型执行逻辑,这取决于它是否是 Individual 或者 Organization ?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题