9. Jetty利用メモ - hikarizhen/research-swagger GitHub Wiki

Jettyについて

  • Connect To Oracle
    ojdbc6.jarを配置
    パス:
    jaxrs-server\src\main\webapp\WEB-INF\lib
  • jetty-env.xmlファイル作成
    配置パス:
    jaxrs-server\src\main\webapp\WEB-INF

jetty-env.xmlファイル


<Configure id='oracledbdemo' class="org.eclipse.jetty.webapp.WebAppContext"> <New id="datasource" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg>jdbc/datasource</Arg> <Arg> <New class="oracle.jdbc.pool.OracleDataSource"> <Set name="DriverType">thin</Set> <Set name="URL">jdbc:oracle:thin:@192.168.1.2:1521:orcl</Set> <Set name="User">ora_user</Set> <Set name="Password">ora_password</Set>
  • web.xmlファイル修正

    src\main\webapp\WEB-INF\web.xml


    <res-ref-name>jdbc/datasource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
  • DBにアクセス

import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;

// DSを取得する方法

        Connection connection = null;
        try {
            InitialContext ctx = new InitialContext();
            DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/datasource");
            if (dataSource == null) {
                System.out.println(">>> dataSource is null");
            } else {
                System.out.println(">>> dataSource is found.");
            }

            // get connection
            connection = dataSource.getConnection();
        } catch (javax.naming.NamingException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

  • Jetty Run ポート修正

Jetty Run起動コマンド

mvn clean package jetty:run

ディフォルトは8080です、

http://localhost:8080/swagger.json

別のポートを利用する場合、pom.xml を下記のように修正する。


        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty-version}</version>
        <configuration>
          <webApp>
            <contextPath>/</contextPath>
          </webApp>
          <webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
          <stopPort>8079</stopPort>
          <stopKey>stopit</stopKey>
          <stopWait>10</stopWait>
          <httpConnector>
       <!-- 
            <port>8080</port>
            ポートを8989に設定する。
       -->
       <port>8989</port>
            <idleTimeout>60000</idleTimeout>
          </httpConnector>
        </configuration>

⚠️ **GitHub.com Fallback** ⚠️