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

2

u/daru567 Sep 03 '20 edited Sep 03 '20

It is basically returning a path which points to the html file and that is what will be returned to whoever calls that API. Usually “html/offer” would be a folder called html under your “main/resources” directory so try putting a simple hello world html file inside like “main/resources/html/offer.html” or maybe “main/resources/static/..” I don’t remember exactly because serving front end display stuff directly from a server call was way in the past if you are planning to build a responsive site. Do you have that file structure in your project? It can also be a custom mapping set in the application-context.xml if i remember correctly that points the real path.

However, since you mentioned html and javascript. You can think of the Spring Webflux as your backend server and html + javascript (frontend) as two separate parts. Your frontend application can be built with popular frameworks such as Angular or ReactJS that uses html and javascript. Your frontend communicates with your backend application with AJAX via RESTful api calls.

1

u/INFLATABLE_CUCUMBER Sep 03 '20

So here's what I've done so far, in terms of getting an identical setup. I've used MVCs before in the .NET framework, so I know the basics of full stack development.

Basically, I copied the "resources" folder in correctly. Then, sort of related to what you said, I added:

<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
	<attributes>
		<attribute name="maven.pomderived" value="true"/>
	</attributes>
</classpathentry>

Into my .classpath file. Then I added

encoding//src/main/resources=UTF-8

into my .settings/org.eclipse.core.resources.prefs file. This still got

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

as an error: "Could not resolve view with name 'html/offer'."

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.