mCs
mCs

Reputation: 2911

Spring boot Auto-generated controllers

In the official Spring tutorial code source coode there are no controller required, so I didn't put them in my sample, however when I try to run my simple app I keep getting for http://localhost:8080/ or for http://localhost:8080/invoice the error:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Mar 09 10:18:51 CET 2017 There was an unexpected error (type=Not Found, status=404). No message available

Structure:

$ 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);
}

and 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. What seems to be the problem?

2. Can this be the lombok causing the issue (user the start.spring.io initializer though)?

EDIT Spring BOot log:

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)

Upvotes: 0

Views: 4320

Answers (1)

Strelok
Strelok

Reputation: 51451

You forgot to annotate your repository with @RepositoryRestResource. If you don't, Spring will not auto generate the url mappings and it will just act as a normal repository. This is a feature of spring-data-rest. So, in your case:

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

Now curl http://localhost:8080/invoices will return all invoices.

Refer to the Spring Data Rest documentation for more info.

Upvotes: 1

Related Questions