Home > OS >  Is it possible to generate Spring hateoas entities with openapi
Is it possible to generate Spring hateoas entities with openapi

Time:01-04

I'm trying to use openapi to generate an enitity that uses the spring-hateoas RepresentationModel class.

Example:

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.hateoas.RepresentationModel;


import java.util.Date;

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "User")
@ToString
public class User extends RepresentationModel<User> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private Integer age;

    @Column(name = "date")
    private Date joinedDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnore
    @ToString.Exclude
    private Chat Chat;
}

I can't find any examples on how to generate an entity like this and for it to extend the RepresentationModel class with type User.

Is this type of generation possible? If so how?

I'm using:

<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0</version>
    <relativePath/> 
</parent>

CodePudding user response:

hateoas is a configurable option/feature in the spring generator.

Add this config option to your openApiGenerate task and it will automatically add the RepresentationModel to your generated models.

<configOptions>
  <hateoas>true</hateoas>
</configOptions>
  • Related