2015-10-10 71 views
0

我有一个spring-boot项目,我使用spring数据休息。我的build.gradle文件看起来像这样。正如你所看到的,我所做的一切都是由manual(好吧,显然不是全部)。如何在spring-boot项目中指定spring-data-rest版本?

我想要的是具有/配置文件链接和能够获取我发布的所有端点的json-schema。相反,我有/ apls链接。所以我已经检查了关于< 2.4版本的spring-data-rest手册,它没有提及/ profile链接和json-schema。所以我认为我没有使用最新版本的spring-data-rest。

我已经添加了spring boot gradle插件,并且我没有为spring-boot-data-rest依赖项指定版本。我也试着添加org.springframework.data:spring-data-rest-webmvc:2.4.0.RELEASE依赖项。

但这显然不起作用,因为我仍然有/阿尔卑斯链接,而不是/ profile链接。

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE") 

    } 
} 

apply plugin: 'java' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 

jar { 
    baseName = 'settings' 
    version = '0.1.0' 
} 

repositories { 
    mavenCentral() 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.3' 
} 
dependencies { 
    compile group: 'org.zeromq', name: 'jeromq', version: '0.3.5' 
    compile group: 'org.json', name: 'json', version: '20141113' 
    compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2' 
    compile group: 'org.skyscreamer', name: 'jsonassert', version: '1.2.3' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-hateoas' 
    compile group: 'postgresql', name: 'postgresql', version:'9.1-901-1.jdbc4' 
    compile("org.springframework.data:spring-data-rest-webmvc:2.4.0.RELEASE") 
    runtime group: 'com.h2database', name: 'h2', version:'1.4.187' 
    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test') { 
exclude(module: 'commons-logging') 
    } 
} 

EDIT1:

我发现,如果我不使用包括依赖

compile("org.springframework.data:spring-data-rest-webmvc:2.4.0.RELEASE") 

比gradle这个弹簧数据休息2.2.3或类似的东西。 但是,当我包含该依赖时,它使用2.4.0就像它应该是,但然后我的测试由于某种原因失败。

我的测试看起来像

package demo; 

import demo.settings.DemoApplication; 
import demo.settings.processing.Channel; 
import demo.settings.processing.ChannelMode; 
import demo.settings.processing.ChannelsController; 
import demo.settings.processing.ChannelRepository; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.SpringApplicationConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 

import java.util.List; 

import static org.junit.Assert.*; 

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = DemoApplication.class) 
public class DemoApplicationTests { 

    final String BASE_URL = "http://localhost:8080/"; 
    private MockMvc mockMvc; 
    @Autowired 
    private ChannelsController controller; 
    @Autowired 
    private ChannelRepository repository; 

    @Before 
    public void setup() { 
     this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); 
    } 

    @Test 
    public void setChannels() { 
     Channel exampleChannel = new Channel(ChannelMode.AUTO, 1, 1, 1, false, 0); 
     controller.setChannels(0, 10, exampleChannel); 
     List<Channel> allChannels = repository.findAllByOrderByBinIndexAsc(); 
     for (int i = 0; i <= 10; i++) { 
      Channel ch = allChannels.get(i); 
      assertEquals(ch.getBinIndex(), i); 
      assertEquals(ch.getC1(), exampleChannel.getC1(), 0); 
      assertEquals(ch.getC2(), exampleChannel.getC2(), 0); 
      assertEquals(ch.getManualCoefficient(), exampleChannel.getManualCoefficient(), 0); 
      assertEquals(ch.getMode().toString(), exampleChannel.getMode().toString()); 
      assertEquals(ch.isExcluded(), exampleChannel.isExcluded()); 
     } 
     exampleChannel.setC1(100); 
     controller.setChannels(0, 11, exampleChannel); 
     allChannels = repository.findAllByOrderByBinIndexAsc(); 
     for (int i = 0; i <= 11; i++) { 
      Channel ch = allChannels.get(i); 
      assertEquals(ch.getBinIndex(), i); 
      assertEquals(ch.getC1(), exampleChannel.getC1(), 0); 
      assertEquals(ch.getC2(), exampleChannel.getC2(), 0); 
      assertEquals(ch.getManualCoefficient(), exampleChannel.getManualCoefficient(), 0); 
      assertEquals(ch.getMode().toString(), exampleChannel.getMode().toString()); 
      assertEquals(ch.isExcluded(), exampleChannel.isExcluded()); 
     } 
    } 
} 

这里是我的仓库

@RepositoryRestResource(path="dts_stm32_settings") 
interface DtsStm32SettingsRepository extends PagingAndSortingRepository<DtsStm32Settings, Long> { 
} 

这里是我的模型

package demo.settings.data_collection.stm; 



import com.fasterxml.jackson.annotation.JsonSubTypes; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.validation.constraints.Max; 
import javax.validation.constraints.Min; 
import javax.validation.constraints.NotNull; 

/** 
* Created by michael on 11/09/15. 
*/ 
@Entity 
public class DtsStm32Settings extends Stm32Settings { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    @NotNull @Min(value=0) @Max(value=65535) 
    private int firstChannelDac; 

    @NotNull @Min(value=0) @Max(value=65535) 
    private int secondChannelDac; 

    @NotNull @Min(value=0) @Max(value=65535) 
    private int dil; 

    @NotNull 
    private CommutatorChannel commutatorChannel; 


    @NotNull @Min(value=0) @Max(value=65535) 
    private int firstChannelPwm; 

    @NotNull @Min(value=0) @Max(value=65535) 
    private int zeroChannelPwm; 

    public DtsStm32Settings() { 
    } 

    public DtsStm32Settings(
      int firstChannelShift, 
      int secondChannelShift, 
      int firstChannelGain, 
      int secondChannelGain, 
      int firstChannelSlope, 
      int secondChannelSlope, 
      boolean led, 
      boolean firstChannelDurationBit, 
      boolean secondChannelDurationBit, 
      int firstChannelDac, 
      int secondChannelDac, 
      int dil, 
      CommutatorChannel commutatorChannel, 
      int firstChannelPwm, 
      int zeroChannelPwm, 
      boolean pulsedPumpMode, 
      int durationOn, 
      int durationOff 
    ) { 
     super(firstChannelShift, secondChannelShift, firstChannelGain, secondChannelGain, firstChannelSlope, secondChannelSlope, led, firstChannelDurationBit, secondChannelDurationBit); 
     this.firstChannelDac = firstChannelDac; 
     this.secondChannelDac = secondChannelDac; 
     this.dil = dil; 
     this.commutatorChannel = commutatorChannel; 
     this.firstChannelPwm = firstChannelPwm; 
     this.zeroChannelPwm = zeroChannelPwm; 
     this.pulsedPumpMode = pulsedPumpMode; 
     this.durationOn = durationOn; 
     this.durationOff = durationOff; 
    } 

    @NotNull 
    private boolean pulsedPumpMode; 

    @NotNull @Min(value=1) @Max(value=65535) 
    private int durationOn; 

    @NotNull @Min(value=0) @Max(value=65535) 
    private int durationOff; 

    public int getFirstChannelPwm() { 
     return firstChannelPwm; 
    } 

    public void setFirstChannelPwm(int firstChannelPwm) { 
     this.firstChannelPwm = firstChannelPwm; 
    } 

    public int getZeroChannelPwm() { 
     return zeroChannelPwm; 
    } 

    public void setZeroChannelPwm(int zeroChannelPwm) { 
     this.zeroChannelPwm = zeroChannelPwm; 
    } 

    public int getFirstChannelDac() { 
     return firstChannelDac; 
    } 

    public void setFirstChannelDac(int firstChannelDac) { 
     this.firstChannelDac = firstChannelDac; 
    } 

    public int getSecondChannelDac() { 
     return secondChannelDac; 
    } 

    public void setSecondChannelDac(int secondChannelDac) { 
     this.secondChannelDac = secondChannelDac; 
    } 

    public int getDil() { 
     return dil; 
    } 

    public void setDil(int dil) { 
     this.dil = dil; 
    } 

    public CommutatorChannel getCommutatorChannel() { 
     return commutatorChannel; 
    } 

    public void setCommutatorChannel(CommutatorChannel commutatorChannel) { 
     this.commutatorChannel = commutatorChannel; 
    } 

    public boolean isPulsedPumpMode() { 
     return pulsedPumpMode; 
    } 

    public void setPulsedPumpMode(boolean pulsedPumpMode) { 
     this.pulsedPumpMode = pulsedPumpMode; 
    } 

    public int getDurationOn() { 
     return durationOn; 
    } 

    public void setDurationOn(int durationOn) { 
     this.durationOn = durationOn; 
    } 

    public int getDurationOff() { 
     return durationOff; 
    } 

    public void setDurationOff(int durationOff) { 
     this.durationOff = durationOff; 
    } 
} 


package demo.settings.data_collection.stm; 

import com.fasterxml.jackson.annotation.JsonSubTypes; 
import com.fasterxml.jackson.annotation.JsonTypeInfo; 

import javax.validation.constraints.Max; 
import javax.validation.constraints.Min; 
import javax.validation.constraints.NotNull; 

/** 
* Created by michael on 11/09/15. 
*/ 

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include=JsonTypeInfo.As.PROPERTY, property = "type") 
@JsonSubTypes({@JsonSubTypes.Type(DtsStm32Settings.class)}) 
abstract class Stm32Settings { 

    @NotNull 
    @Min(value=0) @Max(value=65535) 
    protected int firstChannelShift; 
    @NotNull 
    @Min(value=0) @Max(value=65535) 
    protected int secondChannelShift; 
    @NotNull 
    @Min(value=0) @Max(value=65535) 
    protected int firstChannelGain; 
    @NotNull 
    @Min(value=0) @Max(value=65535) 
    protected int secondChannelGain; 

    @NotNull 
    @Min(value=0) @Max(value=65535) 
    protected int firstChannelSlope; 
    @NotNull 
    @Min(value=0) @Max(value=65535) 
    protected int secondChannelSlope; 
    @NotNull 
    protected boolean led; 
    @NotNull 
    protected boolean firstChannelDurationBit; 
    @NotNull 
    protected boolean secondChannelDurationBit; 

    protected Stm32Settings() { 
    } 


    public int getFirstChannelShift() { 
     return firstChannelShift; 
    } 

    public void setFirstChannelShift(int firstChannelShift) { 
     this.firstChannelShift = firstChannelShift; 
    } 

    public int getSecondChannelShift() { 
     return secondChannelShift; 
    } 

    public void setSecondChannelShift(int secondChannelShift) { 
     this.secondChannelShift = secondChannelShift; 
    } 

    public int getFirstChannelGain() { 
     return firstChannelGain; 
    } 

    public void setFirstChannelGain(int firstChannelGain) { 
     this.firstChannelGain = firstChannelGain; 
    } 

    public int getSecondChannelGain() { 
     return secondChannelGain; 
    } 

    public void setSecondChannelGain(int secondChannelGain) { 
     this.secondChannelGain = secondChannelGain; 
    } 

    public int getFirstChannelSlope() { 
     return firstChannelSlope; 
    } 

    public void setFirstChannelSlope(int firstChannelSlope) { 
     this.firstChannelSlope = firstChannelSlope; 
    } 

    public int getSecondChannelSlope() { 
     return secondChannelSlope; 
    } 

    public void setSecondChannelSlope(int secondChannelSlope) { 
     this.secondChannelSlope = secondChannelSlope; 
    } 

    public boolean isLed() { 
     return led; 
    } 

    public void setLed(boolean led) { 
     this.led = led; 
    } 

    public boolean isFirstChannelDurationBit() { 
     return firstChannelDurationBit; 
    } 

    public void setFirstChannelDurationBit(boolean firstChannelDurationBit) { 
     this.firstChannelDurationBit = firstChannelDurationBit; 
    } 

    public boolean isSecondChannelDurationBit() { 
     return secondChannelDurationBit; 
    } 

    public void setSecondChannelDurationBit(boolean secondChannelDurationBit) { 
     this.secondChannelDurationBit = secondChannelDurationBit; 
    } 

    public Stm32Settings(
      int firstChannelShift, 
      int secondChannelShift, 
      int firstChannelGain, 
      int secondChannelGain, 
      int firstChannelSlope, 
      int secondChannelSlope, 
      boolean led, 
      boolean firstChannelDurationBit, 
      boolean secondChannelDurationBit 
    ) { 
     setFirstChannelShift(firstChannelShift); 
     setSecondChannelShift(secondChannelShift); 
     setFirstChannelGain(firstChannelGain); 
     setSecondChannelGain(secondChannelGain); 
     setFirstChannelSlope(firstChannelSlope); 
     setSecondChannelSlope(secondChannelSlope); 
     setLed(led); 
     setFirstChannelDurationBit(firstChannelDurationBit); 
     setSecondChannelDurationBit(secondChannelDurationBit); 
    } 
} 

这里是错误,告诉我,什么

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stm32Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private demo.settings.data_collection.stm.DtsStm32SettingsRepository demo.settings.data_collection.stm.Stm32Controller.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtsStm32SettingsRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object; 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:687) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) 
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:104) 
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68) 
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) 
    ... 45 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private demo.settings.data_collection.stm.DtsStm32SettingsRepository demo.settings.data_collection.stm.Stm32Controller.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtsStm32SettingsRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object; 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    ... 60 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtsStm32SettingsRepository': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object; 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 
    ... 62 more 
Caused by: java.lang.AbstractMethodError: org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object; 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:185) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) 
    ... 72 more 
+0

正如你所做的那样,显式设置版本应该足以获得2.4.0.RELEASE。你可以使用'./gradlew dependencies'来检查。 –

+0

@AndyWilkinson我有错误的依赖关系。我写了'compole'而不是'compile'。我已经解决了这个问题,现在我的测试失败了。我将编辑该问题。 – user1685095

回答

1

尝试指定春数据版本序列代替:

dependencyManagement { 
    imports { 
    ... 
     mavenBom "org.springframework.data:spring-data-releasetrain:Gosling-RELEASE" 
    } 
} 

然后,只需编译您需要的弹簧数据首先不指定版本!

+0

谢谢,这工作。我会补充说,需要安装spring-dependency-management插件来完成这个任务。 – user1685095

相关问题