将spring-data-rest添加到gs-rest-service示例项目后的依赖性问题

时间:2014-08-12 10:47:44

标签: java maven spring-mvc spring-boot spring-data-rest

我正在尝试使用Spring进行RESTful Web服务,我的首发项目是Spring gs-rest-service。该项目工作正常,当我点击http://localhost:8080/greeting时,我得到{" id":1,"内容":" Hello,World!"}。

问题是当我在spring-data-rest-core中向spring-data-rest-webmvcpom.xml添加依赖项时,该应用程序不再起作用。当我点击http://localhost:8080/greeting时出现以下错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Aug 12 12:27:28 CEST 2014
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

当我删除depndencies应用程序再次正常工作,是什么导致此问题? 这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.5.RELEASE</version>
</parent>

<packaging>war</packaging>

<properties>
    <spring.version>4.0.5.RELEASE</spring.version>
    <spring.boot.version>1.1.4.RELEASE</spring.boot.version>
    <jdk.version>1.7</jdk.version>
    <jetty.version>8.1.8.v20121106</jetty.version>
    <spring.data.rest.version>2.1.2.RELEASE</spring.data.rest.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-core</artifactId>
        <version>${spring.data.rest.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>${spring.data.rest.version}</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>

        <plugin>                
            <groupId>org.mortbay.jetty</groupId>                
            <artifactId>jetty-maven-plugin</artifactId>             
            <version>${jetty.version}</version>             
        </plugin>
    </plugins>
</build>
</project>

3 个答案:

答案 0 :(得分:2)

Spring Boot为Spring Data REST提供了专用的启动程序:

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

答案 1 :(得分:2)

您正在使依赖项和配置中的内容变得复杂。正如@OliverGierke已经提到的那样,使用以下启动器而不是单独的依赖关系。

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

此外,您已经在使用spring-boot-starter-parent所以您不需要弹簧启动版本是您配置的一些其他属性。而不是jdk.version使用java.version而是全部。离开你的下面的pom。 (其中还添加了spring-boot插件)。

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.5.RELEASE</version>
    </parent>

    <packaging>jar</packaging>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

这将创建一个可以与java -jar <your-jar-name-here>.jar一起运行的可执行jar。这也应该包含你需要的所有依赖项,包括用于JSON(un)编组的jackson,这是spring-boot-starter-web的依赖项。

使用@EnableAutoConfiguration的Spring Boot将检测Spring MVC和Jackson并为其进行正确配置。 Spring Data REST也是如此。你自己不应该乱用xml(或战争档案)。

如果您想使用它,我删除了Jetty,请查看Spring Boot reference guide如何执行此操作。

答案 2 :(得分:0)

事实证明,我必须向杰克逊添加依赖项:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

并声明如下Spring MVC - HttpMediaTypeNotAcceptableException所述的以下bean:

<context:annotation-config/>

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

然后,使用@ImportResource("beans.xml")将beans.xml添加到 Application.java