我如何使用 Jetty 为 Angular 应用程序提供服务,并与 Jersey 一起使用同一台服务器?
How can I serve an Angular app using Jetty and also use the same server along with Jersey?
我正在开发一个应用程序,其前端将使用 Angular 开发,后端将使用 Java / Jersey。我使用 Jetty 作为 http 服务器。我的目录结构如下:
src
main
java
com.scibor
Main
SandboxResource
webapp
WEB-INF
web.xml
index.html
bower_components . . .
app.js
在 Main
内,我启动了 Jetty 服务器如下:
Server server = new Server(PORT);
WebAppContext context = new WebAppContext();
context.setResourceBase("src/main/webapp");
context.setContextPath("/");
context.setServer(server);
server.setHandler(context);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
而我的web.xml
配置如下:
<display-name>Web App</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Adds JSON processing to map java classes to JSON text in both directions -->
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- Tell the Jersey framework where to search for services. Also, JAX-RS is needed for the JSON serialization -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.scibor</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>json</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
我的问题是,在此配置下,只有 MyResource
中指定的 RESTful 端点被拾取。但是,如果我转到 localhost:8080/
,它会告诉我找不到资源。如果我将资源库更改为此:
context.setResourceBase("src/main/webapp/index.html");
它现在在我访问 localhost:8080
时为我的 angular 项目提供服务,但它现在没有正确为我的任何 RESTful 端点提供服务。我如何配置它,以便在我访问 localhost:8080
时既可以提供 angular 应用程序,又可以访问 RESTful 端点?
你有一些选择。
一种简单的方法是将带有 index.html 的 angular 基本目录插入后端应用程序的文件夹中。因此,Angular 应用程序的 url 将是 'localhost:8080/app',您的端点将在 'localhost:8080' 上提供服务。这将解决跨源策略问题。
第二种解决方案是使用 grunt-proxy,它将您的端点重定向到不同的端口。 how to use grunt proxy
答案实际上包含在 <url-pattern>/*</url-pattern>
行中。这似乎为 Jersey 指定了基数 URL,但 /*
是索引的基数 URL,因此没有提供任何服务。将其更改为 /server
解决了问题,并让我不仅可以为 angular 应用程序提供服务,还可以访问 Jersey 端点。
我正在开发一个应用程序,其前端将使用 Angular 开发,后端将使用 Java / Jersey。我使用 Jetty 作为 http 服务器。我的目录结构如下:
src
main
java
com.scibor
Main
SandboxResource
webapp
WEB-INF
web.xml
index.html
bower_components . . .
app.js
在 Main
内,我启动了 Jetty 服务器如下:
Server server = new Server(PORT);
WebAppContext context = new WebAppContext();
context.setResourceBase("src/main/webapp");
context.setContextPath("/");
context.setServer(server);
server.setHandler(context);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
而我的web.xml
配置如下:
<display-name>Web App</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Adds JSON processing to map java classes to JSON text in both directions -->
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<!-- Tell the Jersey framework where to search for services. Also, JAX-RS is needed for the JSON serialization -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.scibor</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>json</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
我的问题是,在此配置下,只有 MyResource
中指定的 RESTful 端点被拾取。但是,如果我转到 localhost:8080/
,它会告诉我找不到资源。如果我将资源库更改为此:
context.setResourceBase("src/main/webapp/index.html");
它现在在我访问 localhost:8080
时为我的 angular 项目提供服务,但它现在没有正确为我的任何 RESTful 端点提供服务。我如何配置它,以便在我访问 localhost:8080
时既可以提供 angular 应用程序,又可以访问 RESTful 端点?
你有一些选择。 一种简单的方法是将带有 index.html 的 angular 基本目录插入后端应用程序的文件夹中。因此,Angular 应用程序的 url 将是 'localhost:8080/app',您的端点将在 'localhost:8080' 上提供服务。这将解决跨源策略问题。 第二种解决方案是使用 grunt-proxy,它将您的端点重定向到不同的端口。 how to use grunt proxy
答案实际上包含在 <url-pattern>/*</url-pattern>
行中。这似乎为 Jersey 指定了基数 URL,但 /*
是索引的基数 URL,因此没有提供任何服务。将其更改为 /server
解决了问题,并让我不仅可以为 angular 应用程序提供服务,还可以访问 Jersey 端点。