Jason中的Spring Data Rest -Disable自我链接(HAL)

时间:2015-03-26 08:38:27

标签: java json spring rest spring-mvc

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

{
"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是我在数据库中的另一个表

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

  {
  "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文件。

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。 在此先感谢。

3 个答案:

答案 0 :(得分:0)

您应该确保listOfMerchandiseAttribute是您的域类的成员。然后默认转换为JSON应该在那里。 Spring Data REST将使用您当前的表示形式,并添加超媒体。

另见Disable Hypertext Application Language (HAL) in JSON?

答案 1 :(得分:0)

您可以使用Excerpts(尤其是在这种情况下使用)。因为Spring的例子是如此雄辩,将其复制到这里很愚蠢,所以我只指出它:https://docs.spring.io/spring-data/rest/docs/3.1.x/reference/html/#projections-excerpts.excerpting-commonly-accessed-data
但是为了记录和方便起见,我将粘贴春季示例的主要部分:

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

Projection javadoc处看到types表示投影类型绑定到的类型
摘录可以通过以下方式使用:

@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": ...
}

对您来说merchandise等效于PersonlistOfMerchandiseAttribute等效于address

如何摆脱_links

Disable Hypertext Application Language (HAL) in JSON?处查看我的答案。

最后的记录

Spring不断发展,因此请注意,它至少可以起作用:

spring-data-rest-webmvc 3.1.3.RELEASE

或者,如果您喜欢Spring Boot版本:

spring-boot-starter-parent 2.1.1.RELEASE

答案 2 :(得分:0)

更简洁的方法是在 false 中将以下属性设置为 application.properties,同时创建 Projections

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