r/javahelp • • Sep 03 '20

Any simple github projects that combine Spring Webflux for Java's backend, and HTML + JS for frontend?

Hi, I found something a while ago that did this: https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

https://github.com/ericbv/staticContentWithSpringBoot/tree/master/src/main/resources.

Problem that I couldn't understand with it was that inside of the controller, it said:

@RequestMapping("/offerView")
public String page(){
    return "html/offer";
}

I couldn't make sense of this path "html/offer", and I'd tried to replicate this relatively simple concept and put it into a WebFlux app, but I couldn't get the thing to work. I'm still searching, but looking for some sort of github project, or example for how to do this linkage, and coming up short. Anyone got any ideas? Thanks

20 Upvotes

6 comments sorted by

View all comments

Show parent comments

2

u/daru567 Sep 04 '20

I've downloaded and loaded the github project in the link you posted above. Started it up, went to http://localhost:8080/offerView and the page loads just fine. So it seems that isn't your own project? Do you have your project loaded onto a source repository for easy access? It's difficult to know what you have done.

1

u/INFLATABLE_CUCUMBER Sep 04 '20

This github project isn’t a problem. It works for me too. The problem is that I don’t understand how it works. I’d like to use a similar method of structure to copy this controller into a new webflux app of my own, as well as all of its components in the src/main/resources folder, but I don’t understand how to replicate this portion and make it run similarly. That’s why I’ve also edited classpath and prefs files as mentioned. The github project works fine, but I don’t know how it works, which is something I need in order to replicate it.

2

u/daru567 Sep 04 '20

So you want to serve a static html file with a webflux controller. Try this simple tutorial:

https://www.baeldung.com/spring-webflux-static-content

1

u/INFLATABLE_CUCUMBER Sep 09 '20

Dude! Thanks! That kinda helped a moderate bit!

Baeldung I've had varying returns on. For instance, inside of

public RouterFunction<ServerResponse> htmlRouter(
    @Value("classpath:/public/index.html") Resource html) {
    return route(GET("/"), request
      -> ok().contentType(MediaType.TEXT_HTML).syncBody(html)
    );
}

there are a few things that aren't explained, like where does Resource object come from (it's org.springframework.core.io), or where does ok() come from (ServerResponse.ok()), as well as a couple of other things, like class structure, basic setup, pom.xml dependencies, etc. I just wish they'd give a direct github project attached in their examples. That one turned out to be:

public RouterFunction<ServerResponse> htmlRouter(
   //org.springframework.core.io.Resource
   @Value("classpath:/index.html") Resource html) { //		/public/index
      return RouterFunctions.route(RequestPredicates.GET("/"), request
          -> ServerResponse.ok().contentType(MediaType.TEXT_HTML).syncBody(html)
    );
}

I'm gonna keep working on it. Main stuff now is gonna be alternating between Mono and Flux and understanding how those work.