原本打算用spring mvc的蠢涝,但想想配置太麻煩派阱,所以就選擇了spring boot。因公司要求要采用JAX-RS規(guī)范揩环,所以用了jersey搔弄。 Spring boot已支持jersey,且提供了專門的依賴:spring-boot-start-jersey丰滑,所以整合起來比較簡單顾犹。
Spring boot:2.0.0.RELEASE
JDK:1.8
Jboss:7.0.4
maven:3.3.3
部署:打成war包,發(fā)布到jboss
pom.xml
因為要打成war包吨枉,所以在pom.xml中加上如下代碼:
<packaging>war</packaging>
因為有父modules蹦渣,所以用下面的方式引入spring boot的父依賴,同時也附上了一些主要的依賴貌亭,詳細介紹可以參考官網柬唯。
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Add typical dependencies for a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- 因為要部署到jboss,所以要把內置的tomcat去掉 -->
<!-- 官網的描述:To build a war file that is both executable and deployable into an external container, you need to mark the embedded container dependencies as “provided”-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties
路徑:src/java/resources
Spring boot的默認配置文件是application.properties圃庭。
數據源是用jndi锄奢,下面是spring boot中jndi的配置
spring.datasource.jndi-name=java:jboss/datasources/customers
引入了jersey, 可以使用下面的配置來定義jersey請求的路徑
# Path that serves as the base URI for the application.
spring.jersey.application-path=/jersey
Spring boot自身提供了一些Endpoint, 可以利用這些Endpoint可以查看Spring boot一些自動配置的信息剧腻、beans拘央、health等等,幫助開發(fā)者學習Spring boot的一些自動配置及監(jiān)控我們的Application书在。默認的路徑是/actuator灰伟,如:/actuator/health, /actuator/beans儒旬。有些Endpoint是安全控制的栏账,需要特殊配置,具體可以參考官網栈源〉簿簦可以在properties里面配置下面參數來改變默認路徑:
management.endpoints.web.base-path=/manage
如果要用這些自帶的Endpoint,需要在pom.xml中加入下面的依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
介紹這2個路徑的配置是為了引入一個問題甚垦,在Spring boot 2.0.0之前的版本茶鹃,自帶的Endpoint默認路徑是/ *, 而jersey的默認路徑也是/ *。自帶的Endpoint是基于Spring MVC dispatcher艰亮,jersey是基于jersey dispatcher. 如果2個根路徑是一樣的話闭翩,Spring boot就有可能不知道請求要基于哪個dispatcher來處理迄埃,進而報錯。最新版本應該沒有這個問題调俘,因為Spring boot已經給自帶的Endpoint的默認路徑改為/actuator伶棒。
Jersey注冊自定義的Endpoint
Jersey注入
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(Endpoint.class);
}
}
Endpoint
基于目前的配置,該URL:host:port/[projectname]/jersy/hello
@Component
@Path("/hello")
public class Endpoint {
@GET
public String message() {
return "Hello";
}
}
啟動類
下面這段是引處官網的肤无,我的理解:沒有用內置的tomcat做部署,所以得重寫configure方法宛渐,把外部的web容器上下文引入竞漾。
The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure method. Doing so makes use of Spring Framework’s Servlet 3.0 support and lets you configure your application when it is launched by the servlet container. Typically, you should update your application’s main class to extend SpringBootServletInitializer, as shown in the following example:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Spring Boot會自動掃描@SpringBootApplication所在類的同級包及下級包里的所有bean业岁,所以建議啟動類放在最外層的包下。
jboss相關配置
位置:src/main/webapp/WEB-INF
jboss-web.xml
配置web context信息笔时,web context是URL的根目錄。若不配仗岸,默認項目名為web context允耿。 如下是配置的例子,那么根URL為:host:port/demo/
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
<jboss-web>
<context-root>demo</context-root>
</jboss-web>
jboss-deployment-structure.xml
jboss中扒怖,自帶了一些jar包较锡,spring boot項目中會用到一些jboss中自帶的jar包,但項目中用的jar版本可能和jboss中自帶的jar不匹配盗痒,代碼部署到jboss蚂蕴,啟動過程中會報一些類找不到的錯誤。
有兩種解決辦法:
1. 改jboss中的jar包俯邓,使之與項目匹配
2. 在項目中添加jboss-deployment-structure.xml骡楼,exclude jboss中那些版本不匹配的jar包,再在項目中的pom.xml添加exclude掉的正確版本的jar依賴:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name=“com.fasterxml.jackson.core.jackson-core” />
<module name=“com.fasterxml.jackson.core.jackson-annotations” />
<module name=“com.fasterxml.jackson.core.jackson-data bind” />
<module name=“com.fasterxml.jackson.jaxrs.jackson-jaxrs-Jason-provider” />
<module name=“org.jboss.resteasy.resteasy-jackson2-provider” />
<module name=“org.jboss.resteasy.resteasy-jackson-provider” />
<module name="javax.ws.rs.api"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
當然看成,這兩種方法可能都會引起其他相關聯(lián)jar的版本沖突君编,需要一一排查
調用ejb
在代碼里調用remote ejb跨嘉,啟動jboss時川慌,發(fā)現一些ClassCastException。這個異常祠乃,還是因為包沖突引起的梦重。
在上一步中,exclude掉了jboss中自帶的javax.ws.rs-api亮瓷,在pom.xml里又引入了該jar的2.1版本琴拧,導致jboss中ejb用到的相關class與新版本的javax.ws.rs-api中的class沖突。
因不想再處理這些包沖突嘱支,也因jboss中的ejb還有其他application在用蚓胸,所以干脆降低了springboot版本為1.5.8挣饥,去掉上一步中的<module name="javax.ws.rs.api"/>這句代碼(不引入新版本jar,直接使用jboss自帶的javax.ws.rs-api.jar)沛膳,問題就解決了扔枫。
總結:經常解決完問題后,過些日子就忘記了锹安,年紀大了短荐,不得不服老呀!所以整理一下記下來叹哭,也許能利己也利各方碼農呢...