1

I want to put a Java class in a Maven artifact that uses the Vaadin @Endpoint annotation (from com.vaadin.flow.server.connect.Endpoint), to use this class in multiple Vaadin projects.

Here is a simple example of such a class:

import com.vaadin.flow.server.connect.Endpoint;
import lombok.RequiredArgsConstructor;
import java.util.Optional;

@Endpoint
@RequiredArgsConstructor
public class SupportInfoEndpoint {

  public Optional<String> getSupportInfo(){
    return Optional.of("mailto:support@my.corp");
  }
}

The Maven artifact includes the source code of the class. What do I have to do so this class is scanned in the using project, by the Vaadin build process, so that

  • the corresponding TypeScript code for the frontend is generated
  • the class is included in the Spring-Boot application (so the endpoint is actually available at run time)

Is it possible at all?

ollitietavainen
  • 3,900
  • 13
  • 30
ThomasH
  • 22,276
  • 13
  • 61
  • 62

2 Answers2

5

Like Erik said, it will be implemented with #9010.

But there is a workaround depending on some restrictions. If you have every class that the endpoint needs in the same jar, you could trigger the typescript generation in same the jar by calling the goal "build-frontend" of vaadin-maven-plugin, then the typescript is generated and it's just a matter of some maven magic to move them to META-INF/resources/frontend (something similar of what is being done here). Then you just can package the endpoints in the jar.

For registering the endpoint in the project, you need to do something similar to what this class is doing, basically a ServiceInitListener that will execute the method registerEndpoint of the EndpointRegistry by using reflection.

Dharman
  • 30,962
  • 25
  • 85
  • 135
mmlopez
  • 51
  • 1
  • 4
  • Thanks, Martín, that sounds interesting, I will look into it. – ThomasH Aug 27 '21 at 10:02
  • Great, the `vaadin:build-frontend` task is working nicely (while also doing a whole slew of other things). We currently package the generated typescript with the frontend code that uses it (so its easily found), but I'll keep an eye on packaging it with the backend jar. - For the pure Java endpoint, registration doesn't seem necessary, our endpoint is found and used just fine from its jar. I'll look into the ServiceInitListener approach when we may add the generated typescript to the jar. – ThomasH Aug 27 '21 at 15:11
  • 2
    The good thing about packaging the ts in the backend jar, is that the typescript generation in the front should take less time because you'll skip re-generating things that didn't change (ie: classes in jars). You can still use code completion by importing ts from `@vaadin/flow-frontend/generated` like the addon is doing [here](https://github.com/FlowingCode/AppLayoutAddon/blob/master/src/main/resources/META-INF/resources/frontend/fc-applayout/fc-fusion-layout.ts#L7). – mmlopez Aug 29 '21 at 12:03
  • What is `@vaadin/flow-frontend/generated` pointing to? The `resources/frontend` path in the backend jar? In my Vaadin 19/20 apps' package.json, `@vaadin/flow-frontend` is pointing to `./target/flow-frontend`, and that does not contain a `generated` folder in any of them (but the source tree in `frontend` does)?! – ThomasH Aug 30 '21 at 06:48
  • I've also looked at your `RegisterEndpointServiceInitListener`, but since the endpoint class `MenuEndpoint` does not provide a `registerEndpoint` method that could be called, I don't quite see the effect of the ServiceInitListener?! – ThomasH Aug 30 '21 at 08:17
  • 1
    The `registerEndpoint` method is not on `MenuEndpoint` but in `EndpointRegistry`. That method is called to register the endpoint dynamically. I wrote [an article](https://www.flowingcode.com/en/using-our-applayout-in-a-fusion-application/) with a sample project were you can see the addon working with this mechanism. – mmlopez Nov 16 '21 at 11:31
2

Unfortunately it is not currently possible, but it will be once #9010 is implemented.

It is my understanding that it is one of the highest priority features to implement for the Fusion team.

Erik Lumme
  • 5,202
  • 13
  • 27