概要
手元でさくっと動作確認
確認できたらサーバー(tomcatなど)にデプロイすればいいじゃない?
という事で、両対応できるようにしてみた。
しかもローカルでの起動が超早い特典付き!
IntelliJ コミュニティ版(無料)を使う
Create New Project
を押す
プロジェクト名を投入
Maven を選ぶ
左側の Maven
を選択して Next
を押す
Project SDKが何も無いときは New...
を押してJDKを設定
プロジェクトのフォルダ名を設定
最初の画面
右上の Enable Auto-import
を押す
プロジェクトのファイル一覧を表示
左側の 1:Project
を押すと出てくる
左側に何も表示されていない場合は、左下のアイコンをクリックすると出てくる
必要なライブラリの設定
最初の画面には pom.xml
が表示されている。
そこに必要なライブラリを書き足すと、自動的にライブラリをダウンロードして設定してくれる。
/pom.xml ファイル
http://camel-cap.blogspot.jp/2016/01/apache-camel-hello-rest.html?view=sidebar
サーバーを使わなくても動作確認できるようにしとく
クラス AppMain
の作成
package example.api;
import org.apache.camel.spring.Main;
public class AppMain {
public static void main(String[] args) throws Exception {
System.out.println("-- start! --");
Main main = new Main();
main.run();
}
}
このまま起動するとエラーになるので、spring用ファイルを準備する必要がある
サーバー(tomcat)にデプロイしても動くようにする
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<!-- spring.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/META-INF/spring/*.xml</param-value>
</context-param>
<!-- Start Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
httpリクエストを受け付けるようにspring用ファイルを作成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.16.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- ルートの自動登録(サブディレクトリも検索される) @Componentが対象 -->
<context:component-scan base-package="example.api"/>
<!--ヘッダフィルタを登録-->
<bean name="AllFilter" class="example.api.AllFilter"/>
<!-- camelルート -->
<camelContext xmlns="http://camel.apache.org/schema/spring">
<contextScan/>
<restConfiguration component="netty4-http" contextPath="/api/1" host="0.0.0.0"
port="8888" bindingMode="json">
<!-- httpクエリをexchange.getIn()のheaderに入れる -->
<endpointProperty key="mapHeaders" value="true"/>
<!-- httpクエリをurlデコード-->
<endpointProperty key="urlDecodeHeaders" value="true"/>
<!-- 独自のhttpヘッダを返信しない -->
<endpointProperty key="headerFilterStrategy" value="AllFilter"/>
</restConfiguration>
</camelContext>
</beans>
これで http://localhost:8888/api/1
でhttp受信できる
REST受信
REST受信用のクラスを作成
package example.api.rest;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class HelloRest extends RouteBuilder{
@Override
public void configure() throws Exception {
rest("/hello")
.get().to("direct:hello");
}
}
これで http://localhost:8888/api/1/hello
でREST (get)受信できる
処理の部分を作成
package example.api.route;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@Component
public class HelloRoute extends RouteBuilder{
@Override
public void configure() throws Exception {
from("direct:hello")
.process(exchange -> {
Map map = new HashMap();
map.put("hello", "world");
map.put("one", 1);
map.put("time", Calendar.getInstance());
map.put("aa", new int[]{1,2,3});
exchange.getIn().setBody(map);
});
}
}
httpの独自ヘッダを誤って返信しないようにフィルタを設定
簡単にhttpヘッダを設定できるので、誤って設定した場合でも自動的に削除する設定を入れる。
package example.api;
import org.apache.camel.Exchange;
import org.apache.camel.spi.HeaderFilterStrategy;
public class AllFilter implements HeaderFilterStrategy {
@Override
public boolean applyFilterToCamelHeaders(String headerName, Object headerValue, Exchange exchange) {
// サーバから出力するほうはフィルタする
return true;
}
@Override
public boolean applyFilterToExternalHeaders(String headerName, Object headerValue, Exchange exchange) {
// 入ってくる方のデータはフィルタリングしない
return fall;
}
}
全てのファイル一覧
ローカルで実行
先ほど作成した AppMain
を開くと5行目と7行目にアイコンが付いている
サーバーにデプロイする
warファイルをそのまま持って行くと大変でかいので、ソースコードだけ持っていく。
サーバーで実行
git pull
mvn clean package
mv /helloapi/target/*.war ~/tomcat/webapps/
毎回実行するのですら面倒なので、jenkinsなどで自動化すればいい。
参考
http://camel.apache.org/rest-dsl.html
http://camel.apache.org/netty4-http.html