Java Spring@Projection注释不起作用

Java Spring@Projection注释不起作用,java,spring,rest,maven,Java,Spring,Rest,Maven,我正在学习SpringMVC。我试图通过实现一些网站(如此处)上描述的投影,将我的rest视图限制在某些领域 但我不能让它运行,所以我的项目中可能遗漏了一些东西 我的rest视图现在是这样的: http://localhost:8080/rest/image/1: 还是这个 http://localhost:8080/rest: 但我需要将它们限制为标题和url字段 所以我放置了一个特殊的投影界面到域文件夹和图像实体所在的包。但是什么也没发生。我希望有一个特殊的json值,带有教程中提到的投影和

我正在学习SpringMVC。我试图通过实现一些网站(如此处)上描述的投影,将我的rest视图限制在某些领域

但我不能让它运行,所以我的项目中可能遗漏了一些东西

我的rest视图现在是这样的:

http://localhost:8080/rest/image/1:

还是这个

http://localhost:8080/rest:

但我需要将它们限制为标题和url字段

所以我放置了一个特殊的投影界面到域文件夹和图像实体所在的包。但是什么也没发生。我希望有一个特殊的json值,带有教程中提到的投影和链接字符串,但运气不好。这是一个树和文件:

tk/trzcy/gallery/domain/BasicImage.java

package tk.trzczy.gallery.domain;

import org.springframework.data.rest.core.config.Projection;

@Projection(
        name = "basicImage",
        types = { Image.class })
public interface BasicImage {
    String getUrl();
    String getTitle();
}
package tk.trzczy.gallery.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.trzczy.gallery.domain.Image;
import tk.trzczy.gallery.services.ImageService;
import java.util.List;

@RestController
public class ImageRestController {
    @Autowired
    private ImageService imageService;
    
    @RequestMapping("/rest")
    public List<Image> getWallPage() {
        return imageService.getAllImages();
    }

    @RequestMapping(value = "/rest/image/{id}", method = RequestMethod.GET)
    public Image getImageDetail(@PathVariable("id") int id) {
        return imageService.getImageById(id);
    }
}
package tk.trzczy.gallery.domain;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "images")
public class Image implements BasicImage{
    @Id
    @SequenceGenerator(name = "mySeqGen6", sequenceName = "mySeq6", initialValue = 11, allocationSize = 100)
    @GeneratedValue(generator = "mySeqGen6")

    private Integer id;

    @Column(nullable = false, length = 300)
    private String title;

    @Column(nullable = false)
    private String url;

    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name="image_category_id")
    private ImageCategory imageCategory;

    @Column(name="datecreated", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date dateCreated;

    @PrePersist
    protected void onCreate() {
        dateCreated = new Date();
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }


    public Image() {}

    public Image(Integer id, String title, String url, User user) {
        this.id = id;
        this.title = title;
        this.url = url;
    }

    @Override
    public String toString() {
        return "Post{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", url='" + url + '\'' +
                ", date=" + dateCreated +
                '}';
    }

    public ImageCategory getImageCategory() {
        return imageCategory;
    }

    public void setImageCategory(ImageCategory imageCategory) {
        this.imageCategory = imageCategory;
    }
}

tk/trzcy/gallery/controllers/ImageRestController.java

package tk.trzczy.gallery.domain;

import org.springframework.data.rest.core.config.Projection;

@Projection(
        name = "basicImage",
        types = { Image.class })
public interface BasicImage {
    String getUrl();
    String getTitle();
}
package tk.trzczy.gallery.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.trzczy.gallery.domain.Image;
import tk.trzczy.gallery.services.ImageService;
import java.util.List;

@RestController
public class ImageRestController {
    @Autowired
    private ImageService imageService;
    
    @RequestMapping("/rest")
    public List<Image> getWallPage() {
        return imageService.getAllImages();
    }

    @RequestMapping(value = "/rest/image/{id}", method = RequestMethod.GET)
    public Image getImageDetail(@PathVariable("id") int id) {
        return imageService.getImageById(id);
    }
}
package tk.trzczy.gallery.domain;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "images")
public class Image implements BasicImage{
    @Id
    @SequenceGenerator(name = "mySeqGen6", sequenceName = "mySeq6", initialValue = 11, allocationSize = 100)
    @GeneratedValue(generator = "mySeqGen6")

    private Integer id;

    @Column(nullable = false, length = 300)
    private String title;

    @Column(nullable = false)
    private String url;

    @ManyToOne(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
    @JoinColumn(name="image_category_id")
    private ImageCategory imageCategory;

    @Column(name="datecreated", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date dateCreated;

    @PrePersist
    protected void onCreate() {
        dateCreated = new Date();
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }


    public Image() {}

    public Image(Integer id, String title, String url, User user) {
        this.id = id;
        this.title = title;
        this.url = url;
    }

    @Override
    public String toString() {
        return "Post{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", url='" + url + '\'' +
                ", date=" + dateCreated +
                '}';
    }

    public ImageCategory getImageCategory() {
        return imageCategory;
    }

    public void setImageCategory(ImageCategory imageCategory) {
        this.imageCategory = imageCategory;
    }
}
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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>tk.trzczy</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <aspect.version>1.9.2</aspect.version>
        <jackson.version>2.9.8</jackson.version>
        <hibernate.version>5.4.2.Final</hibernate.version>
        <hibernate.validator.version>5.2.1.Final</hibernate.validator.version>
        <c3p0.version>0.9.5.2</c3p0.version>
    </properties>

    <dependencies>

        <!-- servlets and jps -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>3.0.8</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>jboss</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--hibernate-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- Hibernate-C3P0 Integration -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate.validator.version}</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <!-- for rest services -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>

        <!-- For Aop -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspect.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspect.version}</version>
        </dependency>

        <!-- To Send Email -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.4</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>

        <!--logging-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.13.3</version>
        </dependency>


        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.30</version>
        </dependency>

        <!-- PostgreSQL -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.1.3.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-core -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-core</artifactId>
            <version>3.3.4.RELEASE</version>
        </dependency>


    </dependencies>

    <build>
        <!-- To define the plugin version in your parent POM -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <!-- To use the plugin goals in your POM or parent POM -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>



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

4.0.0
tk.trzczy
SpringWebMVC
0.0.1
战争
UTF-8
1.9.2
2.9.8
5.4.2.最终版本
5.2.1.最终版本
0.9.5.2
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
4.0.1
假如
org.apache.tiles
附加瓷砖
3.0.8
org.slf4j
jcl-over-slf4j
jboss
javassist
文件上传
文件上传
1.3.3
org.apache.commons
公地io
1.3.2
org.hibernate
冬眠核心
${hibernate.version}
org.javassist
javassist
org.hibernate
休眠实体管理器
${hibernate.version}
org.hibernate
hibernate-c3p0
${hibernate.version}
com.mchange
c3p0
${c3p0.version}
org.hibernate
休眠验证器
${hibernate.validator.version}
mysql
mysql连接器java
5.1.34
com.fasterxml.jackson.core
杰克逊数据绑定
${jackson.version}
javax.servlet.jsp
javax.servlet.jsp-api
2.3.3
org.aspectj
aspectjrt
${aspect.version}
org.aspectj
aspectjweaver
${aspect.version}
com.sun.mail
javax.mail
1.5.4
org.springframework.security
spring安全内核
5.2.0.1发布
org.springframework
SpringWebMVC
5.2.0.1发布
org.springframework
德克萨斯州春季
5.2.0.1发布
org.springframework
春季甲虫
5.2.0.1发布
org.springframework.security
spring安全网
5.2.0.1发布
org.springframework.security
spring安全配置
5.2.0.1发布
org.apache.logging.log4j
log4j型芯
2.8.2
org.apache.logging.log4j
log4japi
2.8.2
org.apache.logging.log4j
log4jweb
2.13.3
org.slf4j
slf4j简单
1.7.30
org.postgresql
postgresql
42.2.16
org.springframework
spring上下文支持
5.1.3.1发布
org.springframework.security
spring安全标记库
5.1.2.1发布
org.springframework
spring上下文
5.2.7.发布
org.apache.maven.plugins
maven战争插件
3.2.2
org.apache.maven.plugins
maven surefire插件
2.22.1
org.springframework
弹簧网
5.2.0.1发布
org.springframework.data
spring数据rest内核
3.3.4.1发布
org.apache.maven.plugins
maven surefire插件
2.22.1
org.apache.maven.plugins
maven编译器插件
1.8
1.8
org.springframework.boot
springbootmaven插件