0

My problem is that when I define a relationship in spring data rest it does not return it as it is normally handled and I need it to return in the same standard way without having to consult the link I leave the configuration and the way I am doing. Note: One solution I found is by changing the setter and getters methods for the attribute.

Configuration file:

@Configuration
public class RestConfiguration implements RepositoryRestConfigurer {

    @Autowired
    private EntityManager entityManager;

    @SuppressWarnings("rawtypes")
    @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
            Class[] classes =    entityManager.getMetamodel().getEntities().stream().map(Type::getJavaType)
                .toArray(Class[]::new);
        config.exposeIdsFor(classes);
    }
}

Dependency:

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

Repository:

@RepositoryRestResource(path = "countries", collectionResourceRel = "countries")
public interface CountryRepository extends JpaRepository<Country, Long> {

}

Entity child:

@Entity
@Table(name = "roles")
@RestResource
public class Roles {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String name;
    @Column
    private Boolean status;
    @ManyToOne
    @JoinColumn(name = "country_id", referencedColumnName = "id")
    private Country country;

//setters
//getters
}

Entity parent:

@Entity
@Table(name = "countries")
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String code;
    private String description;
    private String img;
//setters
//getters

}

curl:

curl --location --request GET 'http://localhost:8080/middleware/roles'

enter image description here

  • can you explain the relationship between countries and roles (in words)? I don't think it's very clear in your post. – dsp_user Nov 28 '22 at 19:52

0 Answers0