Spring数据休息 - Json中的禁用自我链接(HAL) [英] Spring Data Rest -Disable self links(HAL) in Json

查看:135
本文介绍了Spring数据休息 - Json中的禁用自我链接(HAL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是spring-data-rest的新手。在我的应用程序中,当我进行休息呼叫时,我正在获取json,其中包括自我链接和外键表的链接。但我想要json而不是链接。我使用telosys工具生成器生成了我的代码。这是我的JSON,当我对数据库中的商品表进行REST调用时,我得到了什么。:

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"
     },
   }
 }

但是我想获得listOfMerchandiseAttribute的链接而不是listOfMerchandiseAttribute的JSON。 listOfMerchandiseAttribute是我在数据库中的另一个表

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

这就是我希望我的json像这样:

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     
     },
   }
 }

何时我搜索谷歌我得到了一些结果,并根据这改变了ApplicationConfig.java文件,但我仍然得到链接而不是JSON。这是My ApplicationConfig文件。

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

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();
    }

}

任何人都可以帮我解决我的问题,并在链接上获取JSON。
先谢谢。

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

推荐答案

你可以使用摘录(特别针对此场景制作)。因为Spring的例子很有说服力而且在这里复制它是愚蠢的,我只想指出: https://docs.spring.io/spring-data/rest/docs/3.1.x/reference/html/# projection-excerpts.excerpting-common-accessible-data

但是为了记录和方便,我将粘贴弹簧示例的主要部分:

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(); 
}

投影javadoc 类型表示投影类型绑定的类型

摘录可以这样使用:

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> {}

为了得到这个:

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

为您商品相当于 Person listOfMerchandiseAttribute 相当于地址

如何摆脱_links

检查我的回答在JSON中禁用超文本应用程序语言(HAL)?

最后的笔记

Spring正在发展,所以请注意这适用于至少:

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

这篇关于Spring数据休息 - Json中的禁用自我链接(HAL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆