Spring boot自动生成的控制器

时间:2017-03-09 09:27:54

标签: spring-boot spring-data-jpa spring-data-rest

在官方的Spring教程代码source coode中,没有控制器需要,所以我没有将它们放在我的示例中,但是当我尝试运行我的简单应用程序时,我一直在寻找{{3} }或http://localhost:8080/错误:

  

Whitelabel错误页面

     

这个应用程序没有/ error的显式映射,所以你看到了   这是一个后备。 Thu Mar 09 10:18:51 CET 2017有一个   意外错误(type = Not Found,status = 404)。没有可用的消息

结构:

$ tree
.
├── domain
│   ├── Invoice.java
│   └── InvoiceRepository.java
├── QbsApplication.java

invoice.java:

package qbs.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Getter
@Setter
@ToString
public class Invoice {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String issuedBy;

    protected Invoice() {
    }
    public Invoice(String issuedBy) {
        this.issuedBy = issuedBy;
    }
}

REPO:

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface InvoiceRepository extends PagingAndSortingRepository<Invoice, Long> {
    List<Invoice> findByIssuedBy(@Param("issuer")String issuer);
}

和APP:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class QbsApplication {

    public static void main(String[] args) {
        SpringApplication.run(QbsApplication.class, args);
    }
}

1。似乎是什么问题?

2。这可能是造成问题的lombok(虽然用户使用start.spring.io初始化程序)?

EDIT Spring BOot日志:

2017-03-09 11:12:43.233  INFO 16837 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-03-09 11:12:43.235  INFO 16837 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-03-09 11:12:43.236  INFO 16837 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-03-09 11:12:43.274  INFO 16837 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-03-09 11:12:43.370  INFO 16837 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2017-03-09 11:12:43.479  INFO 16837 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2017-03-09 11:12:43.481  INFO 16837 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@38ed139b
2017-03-09 11:12:43.695  WARN 16837 --- [           main] org.hibernate.orm.deprecation            : HHH90000014: Found use of deprecated [org.hibernate.id.SequenceGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead.  See Hibernate Domain Model Mapping Guide for details.
2017-03-09 11:12:43.949  INFO 16837 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-03-09 11:12:44.464  INFO 16837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6385cb26: startup date [Thu Mar 09 11:12:40 CET 2017]; root of context hierarchy
2017-03-09 11:12:44.546  INFO 16837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-03-09 11:12:44.547  INFO 16837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-03-09 11:12:44.576  INFO 16837 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-09 11:12:44.576  INFO 16837 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-09 11:12:44.614  INFO 16837 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-03-09 11:12:44.923  INFO 16837 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-03-09 11:12:44.988  INFO 16837 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-03-09 11:12:44.994  INFO 16837 --- [           main] qbs.QbsApplication                       : Started QbsApplication in 4.875 seconds (JVM running for 5.356)

1 个答案:

答案 0 :(得分:1)

您忘记使用@RepositoryRestResource注释您的存储库。如果不这样做,Spring将不会自动生成url映射,它只会充当普通的存储库。这是spring-data-rest的一个特性。所以,在你的情况下:

@RepositoryRestResource(collectionResourceRel = "invoices", path = "invoices")
public interface InvoiceRepository extends PagingAndSortingRepository<Invoice, Long> {
    List<Invoice> findByIssuedBy(@Param("issuer")String issuer);
}

现在curl http://localhost:8080/invoices将返回所有发票。

有关详细信息,请参阅Spring Data Rest documentation