Spring Data Rest -Disable self links(HAL) in Json
Asked Answered
T

4

7

I am newbie to spring-data-rest. In my Application when i make a rest call i am getting json which consist on self-links and links to foreign key tables. But i want json instead on links. I generated my code using telosys tool generator. Here is my JSON what i am gettin when i made a REST call to the "merchandise" table in database.:

{
"id"  : 1,
 "rate": 300,
 "type": "item",
 "shortDescription": "test",
 "longDescription": "test test",

 "_links": {
 "self": {
 "href": "http://localhost:8080/sportsrest/merchandises/1"
 },
 "listOfMerchandiseAttribute": {
 "href": "http://localhost:8080/sportsrest/merchandises/1/listOfMerchandiseAttribute"
     },
   }
 }

But in instead of getting link of "listOfMerchandiseAttribute" i want to get JSON of listOfMerchandiseAttribute. listOfMerchandiseAttribute is my another table in database

That is i want my json like this:

  {
  "id": 1,
 "rate": 300,
 "type": "item",
 "shortDescription": "test",
 "longDescription": "test test",

 "_links": {
 "self": {
 "href": "http://localhost:8080/sportsrest/merchandises/1"
 },
 "listOfMerchandiseAttribute": { 
         "id": 1,
         "attributeName": "testname",
         "attributeValue": 50     
     },
   }
 }

When i search on google i got some results and changed the ApplicationConfig.java file according to that but still i am getting links instead of JSON. Here is My ApplicationConfig file.

ApplicationConfig.java

 /*
 * Created on 19 Mar 2015 ( Time 14:41:07 )
 * Generated by Telosys Tools Generator ( version 2.1.0 )
 */

package co.vitti.sports;

import org.springframework.context.MessageSource;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
import org.springframework.http.MediaType;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import co.vitti.sports.validator.BinaryValidator;
import co.vitti.sports.validator.CustomerPackageBalValidator;
import co.vitti.sports.validator.MerchandiseItemValidator;
import co.vitti.sports.validator.CustomerPackageValidator;
import co.vitti.sports.validator.InvoiceValidator;
import co.vitti.sports.validator.UserRoleValidator;
import co.vitti.sports.validator.SportValidator;
import co.vitti.sports.validator.MerchandiseTypeValidator;
import co.vitti.sports.validator.BookingValidator;
import co.vitti.sports.validator.TenantValidator;
import co.vitti.sports.validator.PaymentModeValidator;
import co.vitti.sports.validator.CourtValidator;
import co.vitti.sports.validator.MerchandisePackageValidator;
import co.vitti.sports.validator.CartValidator;
import co.vitti.sports.validator.MigrationValidator;
import co.vitti.sports.validator.TenantSportValidator;
import co.vitti.sports.repository.converter.TenantSportKeyConverter;
import co.vitti.sports.validator.TenantPaymentmodeValidator;
import co.vitti.sports.repository.converter.TenantPaymentmodeKeyConverter;
import co.vitti.sports.validator.MerchandiseAttributeValidator;
import co.vitti.sports.repository.converter.MerchandiseAttributeKeyConverter;
import co.vitti.sports.validator.CartItemValidator;
import co.vitti.sports.validator.MerchandiseValidator;
import co.vitti.sports.validator.UserValidator;
import co.vitti.sports.validator.TimeslotValidator;
import co.vitti.sports.validator.RoleValidator;

import org.springframework.core.convert.support.ConfigurableConversionService;

/**
 * Application configuration.
 * ( messages resources, validators)
 * @author Telosys Tools Generator
 *
 */
@Configuration
@ComponentScan(basePackageClasses = ApplicationConfig.class)
@EnableWebMvc
public class ApplicationConfig extends RepositoryRestMvcConfiguration {

    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config){
        config.setDefaultMediaType((MediaType) MediaType.parseMediaTypes("application/x-spring-data-verbose+json"));
    }

    @Bean
    public DataSource dataSource(){
        EmbeddedDatabaseBuilder  builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL).build();

    }
    /**
     * Message Ressource declaration.
     * @return MessageRessource
     */
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasename("i18n/messages");
        source.setUseCodeAsDefaultMessage(true);
        return source;
    }

   /**
     * Validator declaration for Binary
     * @return the BinaryValidator
     */
    @Bean
    public BinaryValidator beforeCreateBinaryValidator() {
        return new BinaryValidator();
    }
   /**
     * Validator declaration for CustomerPackageBal
     * @return the CustomerPackageBalValidator
     */
    @Bean
    public CustomerPackageBalValidator beforeCreateCustomerPackageBalValidator() {
        return new CustomerPackageBalValidator();
    }
   /**
     * Validator declaration for MerchandiseItem
     * @return the MerchandiseItemValidator
     */
    @Bean
    public MerchandiseItemValidator beforeCreateMerchandiseItemValidator() {
        return new MerchandiseItemValidator();
    }
   /**
     * Validator declaration for CustomerPackage
     * @return the CustomerPackageValidator
     */
    @Bean
    public CustomerPackageValidator beforeCreateCustomerPackageValidator() {
        return new CustomerPackageValidator();
    }
   /**
     * Validator declaration for Invoice
     * @return the InvoiceValidator
     */
    @Bean
    public InvoiceValidator beforeCreateInvoiceValidator() {
        return new InvoiceValidator();
    }
   /**
     * Validator declaration for UserRole
     * @return the UserRoleValidator
     */
    @Bean
    public UserRoleValidator beforeCreateUserRoleValidator() {
        return new UserRoleValidator();
    }
   /**
     * Validator declaration for Sport
     * @return the SportValidator
     */
    @Bean
    public SportValidator beforeCreateSportValidator() {
        return new SportValidator();
    }
   /**
     * Validator declaration for MerchandiseType
     * @return the MerchandiseTypeValidator
     */
    @Bean
    public MerchandiseTypeValidator beforeCreateMerchandiseTypeValidator() {
        return new MerchandiseTypeValidator();
    }
   /**
     * Validator declaration for Booking
     * @return the BookingValidator
     */
    @Bean
    public BookingValidator beforeCreateBookingValidator() {
        return new BookingValidator();
    }
   /**
     * Validator declaration for Tenant
     * @return the TenantValidator
     */
    @Bean
    public TenantValidator beforeCreateTenantValidator() {
        return new TenantValidator();
    }
   /**
     * Validator declaration for PaymentMode
     * @return the PaymentModeValidator
     */
    @Bean
    public PaymentModeValidator beforeCreatePaymentModeValidator() {
        return new PaymentModeValidator();
    }
   /**
     * Validator declaration for Court
     * @return the CourtValidator
     */
    @Bean
    public CourtValidator beforeCreateCourtValidator() {
        return new CourtValidator();
    }
   /**
     * Validator declaration for MerchandisePackage
     * @return the MerchandisePackageValidator
     */
    @Bean
    public MerchandisePackageValidator beforeCreateMerchandisePackageValidator() {
        return new MerchandisePackageValidator();
    }
   /**
     * Validator declaration for Cart
     * @return the CartValidator
     */
    @Bean
    public CartValidator beforeCreateCartValidator() {
        return new CartValidator();
    }
   /**
     * Validator declaration for Migration
     * @return the MigrationValidator
     */
    @Bean
    public MigrationValidator beforeCreateMigrationValidator() {
        return new MigrationValidator();
    }
   /**
     * Validator declaration for TenantSport
     * @return the TenantSportValidator
     */
    @Bean
    public TenantSportValidator beforeCreateTenantSportValidator() {
        return new TenantSportValidator();
    }
   /**
     * Validator declaration for TenantPaymentmode
     * @return the TenantPaymentmodeValidator
     */
    @Bean
    public TenantPaymentmodeValidator beforeCreateTenantPaymentmodeValidator() {
        return new TenantPaymentmodeValidator();
    }
   /**
     * Validator declaration for MerchandiseAttribute
     * @return the MerchandiseAttributeValidator
     */
    @Bean
    public MerchandiseAttributeValidator beforeCreateMerchandiseAttributeValidator() {
        return new MerchandiseAttributeValidator();
    }
   /**
     * Validator declaration for CartItem
     * @return the CartItemValidator
     */
    @Bean
    public CartItemValidator beforeCreateCartItemValidator() {
        return new CartItemValidator();
    }
   /**
     * Validator declaration for Merchandise
     * @return the MerchandiseValidator
     */
    @Bean
    public MerchandiseValidator beforeCreateMerchandiseValidator() {
        return new MerchandiseValidator();
    }
   /**
     * Validator declaration for User
     * @return the UserValidator
     */
    @Bean
    public UserValidator beforeCreateUserValidator() {
        return new UserValidator();
    }
   /**
     * Validator declaration for Timeslot
     * @return the TimeslotValidator
     */
    @Bean
    public TimeslotValidator beforeCreateTimeslotValidator() {
        return new TimeslotValidator();
    }
   /**
     * Validator declaration for Role
     * @return the RoleValidator
     */
    @Bean
    public RoleValidator beforeCreateRoleValidator() {
        return new RoleValidator();
    }

    /**
     * Add all converters for composite keys
     */
    @Override
    protected void configureConversionService(ConfigurableConversionService conversionService) {
        super.configureConversionService(conversionService);
        conversionService.addConverter(this.tenantsportKeyConverter());
        conversionService.addConverter(this.tenantpaymentmodeKeyConverter());
        conversionService.addConverter(this.merchandiseattributeKeyConverter());
    }

    /**
     * Converter for the composite key in the TenantSport entity
     * @return the converter
     */
    @Bean
    public TenantSportKeyConverter tenantsportKeyConverter() {
        return new TenantSportKeyConverter();
    }
    /**
     * Converter for the composite key in the TenantPaymentmode entity
     * @return the converter
     */
    @Bean
    public TenantPaymentmodeKeyConverter tenantpaymentmodeKeyConverter() {
        return new TenantPaymentmodeKeyConverter();
    }
    /**
     * Converter for the composite key in the MerchandiseAttribute entity
     * @return the converter
     */
    @Bean
    public MerchandiseAttributeKeyConverter merchandiseattributeKeyConverter() {
        return new MerchandiseAttributeKeyConverter();
    }

 // equivalents for <mvc:resources/> tags
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/app/**").addResourceLocations("/app/")
    .setCachePeriod(31556926);
    }

    // equivalent for <mvc:default-servlet-handler/> tag
    @Override
    public void configureDefaultServletHandling(
    DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
    }

}

Can anyone please help me to solve my issue and get the JSON instead on links. Thanks in Advance.

Tetralogy answered 26/3, 2015 at 8:38 Comment(2)
is listOfMerchandiseAttribute its own repository? if it isn't, then you should get the full json, not the linkHerald
@NeilMcGuigan Thanks for replay. listOfMerchandise is not it's own repository. I got the above JSON when i made a REST call to the merchandise table. primary key in merchandise table is foreign key in listOhMerchandiseAttribute. So, i am getting the link of the listOfMerchandiseAttribute table. But instead of link i need to get the JSON of that table.Tetralogy
D
1

You could use Excerpts (especially made for this scenario). Because the Spring example is so eloquent and it's silly to just copy it here I'll just point it: https://docs.spring.io/spring-data/rest/docs/3.1.x/reference/html/#projections-excerpts.excerpting-commonly-accessed-data.
But just for the record and your convenience I'll paste the main parts of the spring example:

@Projection(name = "inlineAddress", types = { Person.class }) 
interface InlineAddress {
  String getFirstName();
  String getLastName();
  Address getAddress(); 
}

see at Projection javadoc that types means The type the projection type is bound to.
The excerpt could be used this way:

@RepositoryRestResource(excerptProjection = InlineAddress.class)
interface PersonRepository extends CrudRepository<Person, Long> {}

in order to get this:

{
  "firstName" : "Frodo",
  "lastName" : "Baggins",
  "address" : { 
    "street": "Bag End",
    "state": "The Shire",
    "country": "Middle Earth"
  },
  "_links": ...
}

For you merchandise is the equivalent of Person and the listOfMerchandiseAttribute is the equivalent of address.

How to get rid of _links

Check my answer at Disable Hypertext Application Language (HAL) in JSON?.

Final notes

Spring is evolving so be aware that this works with at least:

spring-data-rest-webmvc 3.1.3.RELEASE

or, if you prefeer spring boot version:

spring-boot-starter-parent 2.1.1.RELEASE
Defoliant answered 12/12, 2018 at 21:32 Comment(0)
I
0

You should make sure that listOfMerchandiseAttribute is a member of your domain class. Then default converting to JSON should have it there. And Spring Data REST will use what-ever you current representation is and add Hypermedia.

See also Disable Hypertext Application Language (HAL) in JSON?

Ipsus answered 22/6, 2016 at 11:35 Comment(0)
M
0

A cleaner way to do it would be to set the following property to false in application.properties along with creating Projections

spring.hateoas.use-hal-as-default-json-media-type=false

Meanly answered 22/1, 2021 at 5:11 Comment(0)
C
0

I suggest using a custom ResourceProcessor to modify the links that Spring adds. See example in https://mcmap.net/q/671236/-spring-data-rest-integration-with-spring-hateoas Also, if you want to remove the links only on some occasions, you could associate the custom ResourceProcessor with a Projection.

Casi answered 1/6, 2023 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.