0

I'm new to Spring Boot and I bought the book for Spring Boot 2.0 by Greg Turnsquit. In chapter 1 there is a simple application in which if I take a look to http://localhost:8080/chapters URL I'll be able to see the chapter list.

The error I'm having is that on the console the app prints the hashcode of the classes in the repository. In the web browser the app prints a dictionary with three empty elements.

Code Example: https://github.com/learning-spring-boot/learning-spring-boot-2nd-edition-code/tree/master/1

Chapter Controller

package com.greglturnquist.learningspringboot;

import reactor.core.publisher.Flux;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChapterController {

    private final ChapterRepository repository;

    public ChapterController(ChapterRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/chapters")
    public Flux<Chapter> listing() {
        return repository.findAll();
    }
}

Chapter Repository

package com.greglturnquist.learningspringboot;

import org.springframework.data.repository.reactive.ReactiveCrudRepository;

public interface ChapterRepository
    extends ReactiveCrudRepository<Chapter, String> {

}

Loading of Database

package com.greglturnquist.learningspringboot;

import reactor.core.publisher.Flux;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoadDatabase {

    @Bean
    CommandLineRunner init(ChapterRepository repository) {
        return args -> {
            Flux.just(
                new Chapter("Quick Start with Java"),
                new Chapter("Reactive Web with Spring Boot"),
                new Chapter("...and more!"))
            .flatMap(repository::save)
            .subscribe(System.out::println);
        };
    }

}

Fixes I tried

I know sometimes things can be cached in the web browser. I have killed the services running on the port the application is running in. I've also taken a look into the stack trace and there is no error relating to the app itself other than printing the hash of the Chapter objects and not the object information themselves. I'm assuming it might be related to web-flux. However, I'm unsure how the Chapter Repository interacts with web-flux and the embedded MongoDB.

Thanks in advance for any tips and suggestions.

Console log

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v2.0.0.M5)

2018-06-28 11:21:33.581  INFO 1804 --- [           main] c.g.l.LearningSpringBootApplication      : Starting LearningSpringBootApplication on ARD-B3LM5R1 with PID 1804 (C:\DevTraining\Learning-Spring-Boot-2.0-Second-Edition\Chapter01\part1\out\production\classes started by nmartinez in C:\DevTraining\Learning-Spring-Boot-2.0-Second-Edition\Chapter01\part1)
2018-06-28 11:21:33.584 DEBUG 1804 --- [           main] c.g.l.LearningSpringBootApplication      : Running with Spring Boot v2.0.0.M5, Spring v5.0.0.RELEASE
2018-06-28 11:21:33.585  INFO 1804 --- [           main] c.g.l.LearningSpringBootApplication      : No active profile set, falling back to default profiles: default
2018-06-28 11:21:33.680  INFO 1804 --- [           main] .r.c.ReactiveWebServerApplicationContext : Refreshing org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext@5ccddd20: startup date [Thu Jun 28 11:21:33 EDT 2018]; root of context hierarchy
2018-06-28 11:21:34.969  WARN 1804 --- [           main] o.h.v.m.ParameterMessageInterpolator     : HV000184: ParameterMessageInterpolator has been chosen, EL interpolation will not be supported
2018-06-28 11:21:35.383  WARN 1804 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2018-06-28 11:21:35.575  INFO 1804 --- [           main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "{[/chapters],methods=[GET]}" onto public reactor.core.publisher.Flux<com.greglturnquist.learningspringboot.Chapter> com.greglturnquist.learningspringboot.ChapterController.listing()
2018-06-28 11:21:35.578  INFO 1804 --- [           main] s.w.r.r.m.a.RequestMappingHandlerMapping : Mapped "{[],methods=[GET]}" onto public java.lang.String com.greglturnquist.learningspringboot.HomeController.greeting(java.lang.String)
2018-06-28 11:21:35.664  INFO 1804 --- [           main] o.s.w.r.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-06-28 11:21:35.664  INFO 1804 --- [           main] o.s.w.r.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.reactive.resource.ResourceWebHandler]
2018-06-28 11:21:35.684  WARN 1804 --- [           main] o.h.v.m.ParameterMessageInterpolator     : HV000184: ParameterMessageInterpolator has been chosen, EL interpolation will not be supported
2018-06-28 11:21:35.753  INFO 1804 --- [           main] o.s.w.r.r.m.a.ControllerMethodResolver   : Looking for @ControllerAdvice: org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext@5ccddd20: startup date [Thu Jun 28 11:21:33 EDT 2018]; root of context hierarchy
2018-06-28 11:21:36.812  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : note: noprealloc may hurt performance in many applications
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.710-0400 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] MongoDB starting : pid=6980 port=51561 dbpath=C:\Users\NMARTI~1\AppData\Local\Temp\embedmongo-db-250ddd2d-2dba-4972-a86e-decdcbe4df3b 64-bit host=ARD-B3LM5R1
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] db version v3.2.2
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] git version: 6e71d0d568e134c029203593b00a0103e7cdf30b
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] allocator: tcmalloc
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] modules: none
2018-06-28 11:21:36.813  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] build environment:
2018-06-28 11:21:36.814  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten]     distmod: 2008plus
2018-06-28 11:21:36.814  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten]     distarch: x86_64
2018-06-28 11:21:36.814  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten]     target_arch: x86_64
2018-06-28 11:21:36.814  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.712-0400 I CONTROL  [initandlisten] options: { net: { bindIp: "127.0.0.1", http: { enabled: false }, port: 51561 }, security: { authorization: "disabled" }, storage: { dbPath: "C:\Users\NMARTI~1\AppData\Local\Temp\embedmongo-db-250ddd2d-2dba-4972-a86e-decdcbe4df3b", journal: { enabled: false }, mmapv1: { preallocDataFiles: false, smallFiles: true }, syncPeriodSecs: 0.0 } }
2018-06-28 11:21:36.814  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.718-0400 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=0,log_size=2GB),statistics_log=(wait=0),,log=(enabled=false),
2018-06-28 11:21:36.860  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.859-0400 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2018-06-28 11:21:36.861  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.861-0400 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2018-06-28 11:21:36.861  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.861-0400 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/Users/NMARTI~1/AppData/Local/Temp/embedmongo-db-250ddd2d-2dba-4972-a86e-decdcbe4df3b/diagnostic.data'
2018-06-28 11:21:36.904  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:36.904-0400 I NETWORK  [initandlisten] waiting for connections on port 51561
2018-06-28 11:21:36.904  INFO 1804 --- [           main] d.f.embed.process.runtime.Executable     : start de.flapdoodle.embed.mongo.config.MongodConfigBuilder$ImmutableMongodConfig@3f1ddac2
2018-06-28 11:21:37.252  INFO 1804 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:51561], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-06-28 11:21:37.252  INFO 1804 --- [           main] org.mongodb.driver.cluster               : Adding discovered server localhost:51561 to client view of cluster
2018-06-28 11:21:37.297  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:37.297-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51563 #1 (1 connection now open)
2018-06-28 11:21:37.323  INFO 1804 --- [localhost:51561] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:1}] to localhost:51561
2018-06-28 11:21:37.326  INFO 1804 --- [localhost:51561] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:51561, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 2]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=831762}
2018-06-28 11:21:37.328  INFO 1804 --- [localhost:51561] org.mongodb.driver.cluster               : Discovered cluster type of STANDALONE
2018-06-28 11:21:37.731  INFO 1804 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:51561], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-06-28 11:21:37.731  INFO 1804 --- [           main] org.mongodb.driver.cluster               : Adding discovered server localhost:51561 to client view of cluster
2018-06-28 11:21:37.734  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:37.734-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51564 #2 (2 connections now open)
2018-06-28 11:21:37.737  INFO 1804 --- [localhost:51561] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:2}] to localhost:51561
2018-06-28 11:21:37.737  INFO 1804 --- [localhost:51561] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:51561, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 2]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=336344}
2018-06-28 11:21:37.738  INFO 1804 --- [localhost:51561] org.mongodb.driver.cluster               : Discovered cluster type of STANDALONE
2018-06-28 11:21:38.384  INFO 1804 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-06-28 11:21:38.669  INFO 1804 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext     : Started HttpServer on /0:0:0:0:0:0:0:0:9001
2018-06-28 11:21:38.669  INFO 1804 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 9001
2018-06-28 11:21:38.736  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:38.736-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51565 #3 (3 connections now open)
2018-06-28 11:21:38.737  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:38.737-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51566 #4 (4 connections now open)
2018-06-28 11:21:38.739  INFO 1804 --- [      Thread-12] o.s.b.a.mongo.embedded.EmbeddedMongo     : 2018-06-28T11:21:38.738-0400 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:51567 #5 (5 connections now open)
2018-06-28 11:21:38.740  INFO 1804 --- [           main] c.g.l.LearningSpringBootApplication      : Started LearningSpringBootApplication in 5.693 seconds (JVM running for 6.278)
2018-06-28 11:21:38.750  INFO 1804 --- [      Thread-17] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:4}] to localhost:51561
2018-06-28 11:21:38.750  INFO 1804 --- [      Thread-20] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:5}] to localhost:51561
2018-06-28 11:21:38.750  INFO 1804 --- [      Thread-18] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:3}] to localhost:51561
com.greglturnquist.learningspringboot.Chapter@2043a52c
com.greglturnquist.learningspringboot.Chapter@5919de11
com.greglturnquist.learningspringboot.Chapter@393e1e5
NellMartinez
  • 175
  • 9
  • What are you getting back when you call `repository.findAll()`? I don't see anything wrong there. The `de.flapdoodle.embed.mongo` dependency should give you an embedded MongoDB instance to interact with. – x80486 Jun 28 '18 at 14:27
  • Thanks for pointing that out. The repository.findAll() has a value of "FluxOnErrorResume". – NellMartinez Jun 28 '18 at 14:31
  • I just pulled and ran the app for curiosity. It's working fine. It runs on port `9000` by default and returns a JSON array with the IDs and the name for those "chapters". – x80486 Jun 28 '18 at 14:54
  • Interesting. I've killed the service in port 9000 a couple of times. I will restart my computer and download the repo again. Out of curiosity, do you have mongodb installed in your computer or it shouldn't matter for this use case (since we are calling an embedded mongodb)? – NellMartinez Jun 28 '18 at 14:59
  • Will you be able to look to the console output log? I restarted my computer but not luck so far. – NellMartinez Jun 28 '18 at 15:25
  • 2
    You don't need to install anything. Just download the repo, make sure you have Gradle, `cd` into `../1/part1/`, execute `gradle clean build` and then `java -jar build/libs/1/part1.jar`. You should be all set! – x80486 Jun 28 '18 at 19:48
  • Thanks! that worked. It seems to me the fix was intuitive. I will post another question to know how use Gradle with IntellijIdea. I ran the project from this IDE and it doesn't work. Your solution, however, works. Thanks! – NellMartinez Jun 28 '18 at 20:37

0 Answers0