问题
I am considering using Vaadin 12 for my front-end development (we're mainly pure Java engineers and I'd like to keep the code in Java for the most part). However, we may need to also support webservices. If we didn't use Vaadin, we'd just use Jersey & DropWizard, and creating webservices is straightforward.
But in Vaadin 12, can I create webservices with minimal additional code/hacks? (I saw examples of how to do so in Vaadin 8, but Vaadin 10 & 12 is substantively different from what I read.) In particular, any sample code/example that someone could point me to how to do that in Vaadin 12?
(On a related note: Is this a "bad" idea and I should just implement a separate system (using Jersey in DropWizard) for my one or two webservices?...I'd rather not to have to support a whole new server/instance etc just for a couple webservices when 99% of my logic would presumably be in the Vaadin system.)
回答1:
In this sense, Vaadin 8 and Vaadin 10+ are completely the same. If you want to expose for example a REST service, in addition to your Vaadin UI, just expose the REST API to a different URL. E.g. serve them from "/api" if root ( "/" ) is mapped to Vaadin. You can do this either with two separate web apps (war files) or by mapping Vaadin UI and API just to different addresses.
Below is a really simple JAX-RS app that deploys fine next to Vaadin app (also to Vaadin 10+, tested using Wildfly):
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
@ApplicationPath("/api")
public class RestStuff extends Application {
@Path("/")
public static class HelloWorld {
@GET
@Produces("text/plain")
public String getMessage() {
return "Hello World";
}
}
}
回答2:
@mstahv I finally got it to work. My mistake was not using the right jersey libraries. The following libraries worked for me in the end:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
Thanks!
来源:https://stackoverflow.com/questions/54011180/how-can-one-create-webservices-in-vaadin-12