Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, November 2, 2026

AI Tools - AntiGravity・Kiroを使ってみよう

Last updated on:


AIの開発は加速しています。産業への応用も繰り広がり、手軽に使えるようになりました。システム開発においては、具体的な技術には差異がありますが、パターン化しやすい特徴があり、AIの適応性が優れているとも言えるでしょう。ここでは、AIツールを使ってアプリ開発をしてみます。

技術に関する疑問がありましたら、常にGoogleで答えを検索します。まずはGoogleのAIツールを試してみます。

Gemini Code Assistantは2026年6月18日をもってサポートが終了しました。代わりに、AntiGravityという新製品が公表されました。今回はAntiGravityを使ってJavaアプリを生成してみます。今でも多くの企業システムはJava言語で開発されています。



Google AntiGravity


AntiGravityインストール


この記事では、使われている統合開発環境(IDE)はKiro IDEです。Kiro IDEは、VS Codeに基づいて構築され、VS Codeと交換性を持つ開発ツールです。下記のリンクからダウンロードできます。


AntiGravityをKiro IDEで使えるため、AntiGravity拡張機能を導入する必要があります。

導入手順は以下の通りです。

  • Kiro IDEを起動します。
  • 左サイドバーにある拡張機能(Extensions)ボタンをクリックします。
  • 拡張機能検索ボックスにAntiGravityを入力して検索します。
  • 検索結果リストからAntiGravityを特定し、インストールボタンを押下します。



インストールが終わり次第、AntiGravityのアイコンが左サイドバーに現れます。

アイコンをクリックすると、下記の図に示したように、AntiGravityとの会話ボックスが出てきます。そこからメッセージを送ることができます。




新規アプリ作成


最初は、新規アプリを作成します。次のメッセージを送信しました。


「空のSpring Boot RESTアプリAppCreatedByAIを作成してください。」


AntiGravityは、主に次の流れで作業を進めます。

  • 作業ディレクトリーの確認
  • Javaやコンパイル環境等の確認
  • Spring Initializrでのアプリ作成
  • 作成後のアプリのダウンロード
  • アプリの動作確認

動作確認終了後、作成したファイル一式を提示します。

「Accept All」ボタンを押すと、アプリをワークスペースに配置してくれます。

AntiGravityは、タスクを実行する度に、許可を求める仕組みになっています。

途中に「Think [n]s」というメッセージがしばしば目に入ります。



アプリ以外、アプリ実行やテストのコマンドもチャットを経由して提供してくれます。

早速PowerShellを立ち上げ、コマンドでアプリの動作を確認してみました。


アプリの起動

コマンド:.\mvnw.cmd spring-boot:run




REST APIの呼び出し

コマンド:Invoke-RestMethod -Uri "http://localhost:8080/api/hello"




CRUD API一式作成


RESTアプリとは、何かの処理をしてDBへデータを書き込みしたり、DBから取得したデータを変換して提供したり、このような処理を集約したものです。

想定したケースは、Oracle DBの中にapp_usersテーブルがあり、そのテーブル向けの登録と、参照、更新、削除処理を行う一式のAPIを作成することです。とても簡易な機能ではありますが汎用性が高いものです。

この作業を通して、AntiGravityのプログラム設計力を検証したい考えもあります。

AntiGravityへ送られるメッセージについては、すべての情報を纏めた一通でも良いですが、的確に制御できるため、メッセージの粒度を下げ、複数に切り分けます。原則としては、1メッセージに、細分できない1タスクのみを取り込みます。


DDL作成

まず、テーブルのDDLを生成します。次のメッセージを送信します。


「app_usersテーブルのOracle DDLを作成してください。Oracleのバージョンは21Cを想定しています。
テーブルは次の4つカラムを含めています。
カラムid、NUMBER(19)型、Sequenceから値を取得します;
カラムname、VARCHAR2(100)型、必須かつ一意の項目です;
カラムemail、VARCHAR2(100)型、必須項目です;
カラムcreate_at、TIMESTAMP型、CURRENT_TIMESTAMPを付与します。」

結果としては、「schema-app-users.sql」ファイルを作成し、テーブルDDLに加えてカラムのコメントまでも生成してくれました。




DB接続情報

Oracle DBの接続情報は手動で「application.properties」ファイルに記入しておきます。未記入であれば、AntiGravityはあちこち探しに行きます。見つけたらその情報を抽出して「application.properties」に追記します。

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xepdb1
spring.datasource.username=demo
spring.datasource.password=xxxx


物理テーブル作成

DBで物理テーブルを作るために、下のメッセージを送信します。


「application.propertiesファイルの中にあるOracle DBの接続情報を用いて、app_usersテーブルを作成してください。」

AntiGravityは、まずテーブルが存在するか否かを確認します。(今回は新規作成なので、当然存在していません。)

確認が取れたら、先のステップで作成したSQLスクリプトを実行し、テーブルを作成します。



ここでは、作業はまだ終わっていません。

作成したテーブルにテストレコードを挿入したり検証も行います。

検証が終わったら、ロールバックし、テーブルを初期状態に戻します。

品質保証までやってくれました。




APIの作成

いよいよAPI作成の本番にきました。

次のメッセージを送信します。


「app_usersテーブル向けの登録と参照、更新、削除操作を行うREST API一式を作成してください。DBアクセス方式はJDBCにしてください。」

処理ごとにメッセージを分けてもいいですが、粒度は同じなので一つに纏めました。

AntiGravityは、概ね以下の順番に作業を進めます。

  • 構成確認
  • ファイル作成
  • コンパイル
  • テスト実施

各タスクを実行する前に、「このタスクを実行しても良いか」のような許可を得る質問をします。

実は、作成した全てのファイルを一回削除して、APIの再作成を試してみました。所詮、質問もしなく、時間も短縮して出来上げました。作業経験から学び、効率を高めていくことが期待できそうです。



作成済みのファイル一式を確認します。

パッケージやファイルの構成は以下の通りです。

- controller
AppUserController.java
- service
AppUserService.java
- repository
AppUserRepository.java
- model 
AppUser.java
- dto
AppUserRequest.java
AppUserResponse.java
- exception
GlobalExceptionHandler.java
DuplicateResourceException.java
ResourceNotFoundException.java

さすが、教科書のようなマルチレイヤーデザインですね。サービス層にトランザクションの宣言があることも確認できます。

作業メモを鑑みると、テスト実施にはAPIの異常系も検証しました。

品質のいいプログラムを仕上げてくれました。



Amazon Kiro


インフラもコード化に進めてきたクラウドは、AIツールが大活躍できる領域です。Amazonもいくつかの製品を提供しています。Q Developerはその中の一つです。ただし、Qもサポート終了を迎えました。2026年5月15日からは新規アカウントの作成はできなくなりました。既存のアカウントを持っていれば、まだ使えます。また、AWSのアカウントがあれば、コンソールでも利用可能です。

Kiroは、Amazonが推進している製品です。Kiro IDEを使っているなら、IDEに組み込み済みなので、右側のサイドバーからメッセージを送信できます。VS Codeの場合は、別途Kiro CLI拡張機能をインストール必要があります。

この記事では、Kiroを使ってLambda関数を作成してみます。Lambdaは主要なコンピューティングサービスの一つであり、業務ロジックが実装されるところです。

まず、DynamoDBにテーブルを作成するLambda関数を作成してみます。

「DynamoDB向けのMusicテーブルを作成するPython言語のLambda関数を生成してください。パーティションキーはArtist、ソートキーはSongTitleです。」

というメッセージを入力しました。



間もなく、作業を終えて、関数定義のあるPythonファイルを表示してくれました。

エラーハンドリングはお見事です。



続いて、テーブルへ新規レコードを登録する関数を作成します。

以下のメッセージを送信します。Python言語の指定も省きました。


「Musicテーブルにレコードを登録するLambda関数を作成してください。」


関数のコメントには呼び出し例も記されています。分かりやすいです。

コードの品質はもちろん、AWSへデプロイ際の要点や権限設定なども印象深いでした。


全てのソースコードはGitHubからダウンロードできます。


Google AntiGravityもAmazon Kiroも、完成度の高いプログラムを作成できます。効率を高めると同時にコストダウンも可能にしました。ついに、現在のシステム開発構図への影響を考え始めました。


Wednesday, November 5, 2025

[Tips] Spring Boot - React Calls REST APIs Built on WebFlux with API Key Authentication

Last updated on:

 

When building a web app, we’d have a variety of considerations. Functions, along with operability, are what we must include; ease of use is always something that we should pursue; fancy graphical interfaces are a feature that we’d better to have to impress users. Mobile device compatibility is another input we should take into account. Take a look at how many smartphones and tablets are sold every year. It seems to become more and more demanding.

React is one of the most popular libraries for building frontend apps. Technically speaking, React encapsulates rendering logic and markup in components; hence it achieves excellence of interactivity. Facebook and Instagram are the representative examples. 

On the other hand, Java maintains as one of the top languages for building enterprise apps. Not to mention, there are enormous Java apps running in datacenters. With Spring framework, we can build REST APIs in an efficient and effective manner.

If we look beyond, REST APIs can be an effective tool to integrate enterprise apps. Through them, we can provide access to the existent apps or data, can extend the capability to support more flexible and loosely-coupled architectures. When we introduce a new app to the current portfolio or employ a new architecture or develop the enterprise blueprint, it is almost inevitable to look into integration.

The combination of these technologies appears to be an attractive option in terms of developing an enterprise app.

In the meanwhile, secure access to APIs should be ensured, meaning an authentication mechanism should be put into place.


In this demo, we are going to build:

  • A WebFlux app which publishes REST APIs (KeyAccessRestApi project);

  • A React client which consumes the APIs (reactclient project).

With regard to authentication, we’ll get API key access implemented.

Our first try is to display all the departments on the front page. This is what it looks like.



The demo projects can be downloaded from GitHub. 



Prerequisites


To create a WebFlux app, we’ll need Spring Tool Suite. In case you use Eclipse as the IDE, go to Eclipse Marketplace, find and install it. 

To do so, in the Help menu , click Eclipse Marketplace, type “Spring Tool Suite” in the Find box, and click Go button. 

Locate Spring Tools (aka Spring Tool Suite) [Version, for example, 4.32.1.RELEASE] and then click Install button.

Moreover, the below tables need to be created beforehand. 

  • Departments - Holds the list of the departments that will be displayed on the front page.

  • App_ApiKey - Keeps all the valid keys issued to API users.


SpringMVC project/schema.sql
CREATE TABLE Departments (
   Department_Id NUMBER(4)
 , Department_Name VARCHAR2(20)
 , Manager_Id NUMBER(6)
)
 NOLOGGING
 PARALLEL;

CREATE TABLE App_ApiKey (
   Key VARCHAR2(64) UNIQUE
  ,Username VARCHAR2(30) NOT NULL
  ,Description VARCHAR2(128)
)
 NOLOGGING;

The data classes associated with these tables are DptDto and ApiKeyDto, defined in the DptDto.java and ApiKeyDto.java files, respectively.

For the demonstration, we load some test data into the tables.


SpringMVC project/schema.sql
INSERT INTO Departments
WITH tabrows AS (
 SELECT 10, 'Administration', 100 FROM DUAL UNION ALL
 SELECT 20, 'Marketing', 200 FROM DUAL UNION ALL
 SELECT 30, 'Compliance', 300 FROM DUAL UNION ALL
 SELECT 40, 'Channel', 400 FROM DUAL UNION ALL
 SELECT 99, 'Dummy', null FROM DUAL
)
SELECT * FROM tabrows;

INSERT ALL
 INTO App_ApiKey 
  VALUES('XvcItqrOmhPb2gxalIiGWhe7kOQ5cT6E5ZpUpYLM8RU=', 'DEMO', 'Key for user DEMO')
 INTO App_ApiKey 
  VALUES('nXW29eDKVKZnwT2Hu5LXVHp+m0Xd32py9OdRp0qSZIw=', 'TEST', 'Key for user TEST')
SELECT * FROM DUAL;


Create a WebFlux App


To create a WebFlux app, from the main menu, select New > Others, and then choose Spring Starter Project.

In the New Spring Starter Project dialog box, type KeyAccessRestApi as the project name.

In the Dependencies page, check Spring Reactive Web, Srping Data JDBC, Oracle Driver, Spring Security options.

Click Finish button.

Here you go. The KeyAccessRestApi project has been created. It is empty at this point.



Route the Request to the Handler Function


We build a RouterFunction bean to map the API path to the handler, which processes the request and returns the set of departments to the client.

Take a look at the example below, RouterFunctions.route, which indicates:

API Method: GET

Path: /rest/findalldpts (AppConstant.REST_ROOT + AppConstant.API_DPT_FINDALL)

Handler Function: findAllDpts


KeyAccessRestApi project/DptRouter.java
@Configuration(proxyBeanMethods = false)
public class DptRouter {
	@Bean(name = "dptroute")
	public RouterFunction<ServerResponse> route() {

	    return RouterFunctions
	      .route(GET(AppConstant.REST_ROOT+AppConstant.API_DPT_FINDALL)
	    		  .and(accept(MediaType.APPLICATION_JSON)), 
	    		  this::findAllDpts);
	  }
	
	@Autowired
	DptService dptService;
	
	public Mono<ServerResponse> findAllDpts(ServerRequest request) {
		List<DptDto> listDpt = dptService.findAllDpts();
		
		if (listDpt != null && !ObjectUtils.isEmpty(listDpt)) {
			return ServerResponse.ok()
					.contentType(MediaType.APPLICATION_JSON)
					.body(BodyInserters.fromValue(listDpt));
		} else {
			return ServerResponse.notFound()
					.build();
		}
	}
}

The constants used to indicate the route are defined in the AppConstant class.


KeyAccessRestApi project/AppConstant.java
public final class AppConstant {
	public static final String REST_ROOT = "/rest";
	public static final String API_DPT_FINDALL = "/findalldpts";
}


Service


As we don’t have more business logics included in this server app, the service bean, supposed to implement them in a multi-layer structure, simply functions like a wrapper to access the DAO.


KeyAccessRestApi project/DptService.java
@Service
public class DptService{
	@Autowired
	DptDao dptDao;
	
	public List<DptDto> findAllDpts() {
		List<DptDto> dptlist = dptDao.findAllDpts();		
		return dptlist;
	}
}


DAO


This is the actual interface to interact with the database. Typically, we instantiate a JDBC template, and then use it to execute the SQL query and to save the result dataset to our POJOs, shown in the following code.


KeyAccessRestApi project/DptDaoImpl.java
@Repository
public class DptDaoImpl implements DptDao {

	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public List<DptDto> findAllDpts(){	
		String sql = "SELECT department_id, department_name, manager_id "
				+ "FROM departments "
				+ "ORDER BY department_id";
	    	
		List<DptDto> dptlist = null;
		try {
			dptlist = jdbcTemplate.query(sql,
					(rs, rowNum) -> new DptDto(
							rs.getLong("department_id"), 
							rs.getString("department_name"), 
							rs.getInt("manager_id"))
			);
		} catch (Exception e) {
			System.out.println(e);
		}
		
		return dptlist;
	}
}

You can also refer to the previous blog for how to build WebFlux based REST APIs,  

Note that  the data sources are manually configured in that project.



Generate a Secret Key


We are going to make the key generation as a public service, by declaring an API as follows:

HTTP Method: GET

Path: /apputil/genkey (AppConstant.APPUTIL_ROOT + AppConstant.API_GEN_KEY)

Handler Function: generateAesKey

So, we can access the endpoint to get a key either using a web browser or issuing a CURL command.


CURL -X GET http://localhost:9090/apputil/genkey

Class KeyGenerator is used to generate secret keys. It supports algorithms such as “AES”, “DES”, and “DESede”. “AES” is the one that we chose for this demo. For the implementation, refer to the handler, generateAesKey.

This is a part of our system utilities, meaning it is separate from the app’s business functions. Therefore, we create another router for it, which is AppUtilRouter.


KeyAccessRestApi project/AppUtilRouter.java
@Configuration(proxyBeanMethods = false)
public class AppUtilRouter {
	@Bean(name = "apputilroute")
	public RouterFunction<ServerResponse> route() {
		return RouterFunctions
		.route(GET(AppConstant.APPUTIL_ROOT+AppConstant.API_GEN_KEY)
		.and(accept(MediaType.APPLICATION_JSON)), this::generateAesKey);
	}

	public Mono<ServerResponse> generateAesKey(ServerRequest serverRequest) {
		SecretKey  apiKey = null;
		
		try {
			KeyGenerator keyGen = KeyGenerator.getInstance("AES");
			keyGen.init(256); // 256-bit AES key
			apiKey = keyGen.generateKey();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		
		return ServerResponse.ok()
		    .contentType(MediaType.APPLICATION_JSON)
		    .bodyValue(apiKey==null? "failed to generate an access key.":apiKey);
	}
}


Manage the Keys in a Database Table


It is common that we manage the keys in a safe place. At the same time, the feasibility to access them is another factor that we need to consider. Database is a suitable choice for meeting these requirements.

Accordingly, we define a DAO to fetch all the keys from the table in the database.

Here are the keys in the App_ApiKey table.



Below is the DAO definition.


KeyAccessRestApi project/ApiKeyDaoImpl.java
@Repository
public class ApiKeyDaoImpl implements ApiKeyDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Override
	public List<String> getApiKeys() {
		String sql = "SELECT key, username, description "
				+ "FROM app_apikey "
				+ "ORDER BY username";
	    	
		List<ApiKeyDto> keylist = null;
		try {
			keylist = jdbcTemplate.query(sql,
					(rs, rowNum) -> new ApiKeyDto(
							rs.getString("key"),
							rs.getString("username"),
							rs.getString("description"))
			);
		} catch (Exception e) {
			System.out.println(e);
		}
		
		List<String> validKeys = new ArrayList<String>();
		if (keylist != null && !keylist.isEmpty()) {
			keylist.forEach(key -> {
				validKeys.add(key.getKey());
			});
		}
		return validKeys;
	}
}


Add a Custom Filter to Authenticate the Key


Let’s first think about what the filter needs to do in this authentication scenario. It can be summarized as follows.

Step 1: Extract the secret key from the header of a request.

Step 2: Check if the extracted key is valid.

Step 3: If valid, authorize the access; if not, decline it.


Step 1 is implemented in serverAuthenticationConverter. Step 2 and 3 go to reactiveAuthenticationManager.

Then, we create a web filter of type AuthenticationWebFilter to wrap the above beans, which is authenticationWebFilter.

Finally, we add this custom filter to our ServerHttpSecurity by using the addFilterAt method. So that every incoming request has to go through it first. 


KeyAccessRestApi project/SecurityConfig.java
@Configuration(proxyBeanMethods = false)
@EnableWebFluxSecurity
public class SecurityConfig {
	@Bean
	public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
		AuthenticationWebFilter authenticationWebFilter = 
			new AuthenticationWebFilter(reactiveAuthenticationManager());
		authenticationWebFilter.setServerAuthenticationConverter(
			serverAuthenticationConverter());
        
		// Disable CSRF for API key based auth
		http.csrf(ServerHttpSecurity.CsrfSpec::disable);
		http.authorizeExchange(exchanges -> exchanges
		    // App Utility endpoint
		    .pathMatchers(AppConstant.APPUTIL_ROOT + "/**").permitAll()
		    // All other endpoints require authentication
		    .anyExchange().authenticated());
		http.addFilterAt(authenticationWebFilter,
			SecurityWebFiltersOrder.AUTHENTICATION)
		    .httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
		    .formLogin(ServerHttpSecurity.FormLoginSpec::disable);
		
		return http.build();
    }

	@Bean
	public ReactiveAuthenticationManager reactiveAuthenticationManager() {
    		//
    		// Get the valid keys from the database
    		List<String> validApiKeys = apiKeyDao.getApiKeys();
    		
    		return authentication -> {
    			String apiKey = authentication.getCredentials().toString();
    			if (validApiKeys.contains(apiKey)) {
    				return Mono.just(
					new UsernamePasswordAuthenticationToken(apiKey, 
					  apiKey,
					  Collections.singletonList(
					  new SimpleGrantedAuthority("ROLE_USER"))));
    			}
    			return Mono.error(new BadCredentialsException("Invalid API Key"));
    		};
	}

	@Bean
	public ServerAuthenticationConverter serverAuthenticationConverter() {
	return exchange -> 
		Mono.justOrEmpty(
		  exchange.getRequest().getHeaders().getFirst(AppConstant.API_KEY_HEADER))
		.map(apiKey -> new UsernamePasswordAuthenticationToken(apiKey, apiKey));
	}
}


Don’t Forget CORS Configuration


Cross-Origin Resource Sharing (CORS) is a browser security mechanism that controls resource access crossing separate domains. To be more specific, if we’d like to enable the server to accept requests from another domain, by default this is not allowed, we’ll need to change the CORS setting in the server to grant the permission to that domain.

This is a concern that we should address in our demo, meaning we need to get the WebFlux app configured properly in order to accept and process requests from the React client. 

To do so, we create a CorsConfigurationSource bean and then add the client’s origin, "http://localhost:3000", to the allowed list, as depicted below.


KeyAccessRestApi project/SecurityConfig.java
@Configuration(proxyBeanMethods = false)
@EnableWebFluxSecurity
public class SecurityConfig {

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://localhost:3000"); // "*": Allow all origins
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return source;
    }
}


Create a React App


Create React App has been officially deprecated as of Feb. 2025. The React team recommends us to build a new React app with more sophisticated frameworks such as Next.js, React Router, Expo etc. 

We are going to create one based on Next.js. We can do this by using the “create-next-app” command.


npx create-next-app@latest reactclient

After the creation, we add a few files to the project.

  • department.tsx – Defines the Department component, indeed a React function.

  • apidpt.ts – Defgnes the axios APIs that call the REST APIs.

  • constant.js – Defines the constants used in the app.



Build an Axios API to Consume the WebFlux REST API


ApiFindAllDpts is the axios API that retrieves the dataset of all departments.

Frist, we call getAxios to create an axios instance which has the key embedded in its header.

Then, we use the instance’s get method to access the endpoint in the backend app.


reactclient project/apidpt.ts
export function ApiFindAllDpts() {
  const axiosInstance = getAxios();
  return axiosInstance.get(PathFindAllDpts);
}

/*
 * Create an Axios instance
 */
function getAxios(){
  return axios.create({
      baseURL: RestEndpoint,
      headers: {
          "Api-Key": ApiAccessKey,
      },
  });
}


Below is the definition of the constant variables, RestEndpoint and ApiAccessKey and PathFindAllDpts.


reactclient project/constant.ts
/* Root endpoint of the REST APIs */
export const RestEndpoint = 'http://localhost:9090/rest';
export const ApiAccessKey = "XvcItqrOmhPb2gxalIiGWhe7kOQ5cT6E5ZpUpYLM8RU="
export const PathFindAllDpts = '/findalldpts';


Wrap the Data Fetch Operation in a useEffect Hook


Fetching data from an external source is what useEffect is designed for. A classical use case.

ApiFindAllDpts calls the backend REST API and returns a dataset of departments. Each department object contains department ID, department name and manager ID fields.

Considering the editing features that we’ll implement, we add the following additional fields to each department item (Refer to transformDptList).

  • checked: Indicates whether the item is selected. It is bound with the Checkbox.

  • updated: Indicated whether the item’s department name or manager ID has been edited.

  • id: Identifier of the item.

And we declare a state variable dptList to keep the list of these adjusted items.


reactclient project/department.tsx
export default function Department(){	
  const [dptList, setDptList] = React.useState(null);
  …
  /*
   * Get all the departments from the backend
   */
  React.useEffect( () => {
    let ignore = false;
    ApiFindAllDpts()
      .then(response => {
        if (!ignore) {
          transformDptList(response.data);
        }
      })
      .catch(error =>{
        console.error(error);
      });

    return () => {
      ignore = true;
    }
  }, []);

  /*
   * Add the additional fields to each department object
   */
  const transformDptList = (dptlist) => {
    setDptList(dptlist.map((dpt) => 
      dpt={...dpt, id:dpt.department_id, checked:false, updated:false}));
  };
  …
}


Present the List of Departments


This is the markup part of the Department component, which is rendered on the page by React. 

A table is used to display the entire list of departments. We use map function to iterate the items. Each of them shows up on the page with four columns:

  • Checkbox
  • Department ID
  • Department Name
  • Manger ID

Both Department Name and Manager ID are presented with “input” elements. When the Checkbox is selected, those elements become editable. So, we can update the selected department accordingly. 


reactclient project/department.tsx
export default function Department(){
  const [dptList, setDptList] = React.useState(null);
        …
        <tbody>
          {
            dptList && dptList.map((dpt, index) => (
              <tr className="contents" key={index}>
                {/* Checkbox Column */}
                <td className="contents" >
                  <input type="checkbox"
                    id={dpt.id}
                    checked={dpt.checked}
                    onChange={() => handleCheckboxChange(dpt.id)}/>
                </td>
                {/* Department Id Column */}
                <td className="contents" >{dpt.department_id}</td>
                {/* Department Name Column */}
                <td className="contents" >
                  <input type="text"
                    readOnly={!dpt.checked}
                    name="department_name"
                    value={dpt.department_name}
                    onChange={(e)=>onUpdateChange(e, dpt.department_id)} />
                </td>
                {/* Manager Id Column */}
                <td className="contents" >
                  <input type="number"
                    readOnly={!dpt.checked}
                    name="manager_id"
                    value={dpt.manager_id}
                    onChange={(e)=>onUpdateChange(e, dpt.department_id)} />
                </td>
              </tr>
              )
            )
          }
        </tbody>
        …
}


Try the Full Set of CRUD Operations


We can incorporate more functions, the likes of creation and update and deletion, into the app. To do so, we  add the corresponding routes and handlers to the router class.

Here is what the class looks like.


KeyAccessRestApi project/DptRouter.java
@Configuration(proxyBeanMethods = false)
public class DptRouter {
	@Bean(name = "dptroute")
	public RouterFunction<ServerResponse> route() {
	    return RouterFunctions
	      .route(GET(AppConstant.REST_ROOT + AppConstant.API_DPT_FINDALL)
	    		  .and(accept(MediaType.APPLICATION_JSON)), 
	    		  this::findAllDpts)
	      .andRoute(GET(AppConstant.REST_ROOT + AppConstant.API_DPT_FINDBYID), 
	    		  this::findById)
	      .andRoute(POST(AppConstant.REST_ROOT + AppConstant.API_DPT_INSERT), 
	    		  this::insertDpt)
	      .andRoute(PUT(AppConstant.REST_ROOT + AppConstant.API_DPT_UPDATE), 
	    		  this::updateDpt)
	      .andRoute(DELETE(AppConstant.REST_ROOT + AppConstant.API_DPT_DELETE), 
	    		  this::deleteDpt);
	  }
	@Autowired
	DptService dptService;
	
	public Mono<ServerResponse> findAllDpts(ServerRequest request) {
		…
	}
	
	public Mono<ServerResponse> findById(ServerRequest request){
		…
	}

	public Mono<ServerResponse> insertDpt(ServerRequest request){
		…
	}
	
	public Mono<ServerResponse> updateDpt(ServerRequest request){
		…
	}
	
	public Mono<ServerResponse> deleteDpt(ServerRequest request){
		…
	}
}

In the React client, we declare the axios APIs that access those CRUD endpoints, shown in the following code block.


reactclient project/dptapi.ts
export function ApiFindDptById(department_id) {
  const axiosInstance = getAxios();
  const apipath = PathFindDptById + '/' + department_id;
  return axiosInstance.get(apipath);
}

export function ApiInsertDpt(dpt) {
  const axiosInstance = getAxios();
  console.log(">> axios instance: ", axiosInstance);
  return axiosInstance.post(PathInsertDpt, dpt);
}

export function ApiUpdateDpt(dpt) {
  const axiosInstance = getAxios();
  console.log(">> axios instance: ", axiosInstance);
  return axiosInstance.put(PathUpdateDpt, dpt);
}

export function ApiDeleteDpt(department_id) {
  const axiosInstance = getAxios();
  const apipath = PathDeleteDpt + '/' + department_id;
  return axiosInstance.delete(apipath);
}


reactclient project/constant.ts
/* root endpoint of the rest apis */
export const RestEndpoint = 'http://localhost:9090/rest';
export const ApiAccessKey = "XvcItqrOmhPb2gxalIiGWhe7kOQ5cT6E5ZpUpYLM8RU="

export const PathFindAllDpts = '/findalldpts';
export const PathFindDptById = '/finddptbyid';
export const PathInsertDpt = '/insertdpt';
export const PathUpdateDpt = '/updatedpt';
export const PathDeleteDpt = '/deletedpt';

Surely, we need to update the Department component as well; but let’s focus on what we get on the GUI here. 

How the events are processed, how the state variables are handled, refer to department.tsx for more details.



Create a department


Click Create button -> Fill out the input boxes -> Click Submit button.




Search for a department


Enter a department ID -> Click Search button.



Update a department


Check a checkbox to select a department -> Modify its department name or manager ID or both -> Click Update button.




Delete a department


Check a checkbox to select a department -> Click Delete button.




How about the Traffic?


As you might have noticed, the secret key is exposed on the internet. This is a big blow from the perspective of security. To reinforce it and come up with a comprehensive solution, we have to secure the traffic across networks. 

To do so, we can introduce HTTPS to the backend app and only allow the access through HTTPS to the endpoints.



Recap


Leveraging React and Java-based REST API seems to be a practicable technical solution which  can meet various needs in an enterprise context. Needless to say, an authentication mechanism is required to be in place. API key authentication is one of the options. To implement it:

  • On the client side, we embed the key into the request header;

  • On the server side, we build a custom web filter which extracts and validates the key.

However, to make it a sophisticated solution, we’ll need to encrypt the traffic between networks as well.


Wednesday, August 6, 2025

[Tips] Spring Boot - Check Out Spring MVC through Implementing a Simple Function

Last updated on:

 

When it comes to Spring MVC, the well-known MVC architecture diagram comes to our mind. It helps us understand how the parts, DispatcherServlet, Controller, Handler, ViewResolver etc, participate into the process of responding to a web request. With Spring Boot, which is characterized with auto-configuration, for example, the web.xml file where we used to configure those web components even is not generated in a Spring Boot Starter project, how does the MVC model work?  

We’re going to check it out by walking through the following example. What will we do? Fetch all the records in the Employees table and display them on a web page. To realize this, we’ll build a Spring web app with a view, a controller, and a model.


Records in Employees


Display on the view


This sample project is available on GitHub.



Prerequisites


Spring Tool Suite is a must have. Assume Eclipse is the development tool. You can go to Eclipse Marketplace, search and get it installed.

The table, Employees, has been created in the database. Here we’re going to use Oracle XE.


SpringMVC project/schema.sql
-- DDL
CREATE TABLE Employees (
   Employee_Id NUMBER(6)
 , First_Name VARCHAR2(20)
 , Last_Name VARCHAR2(20)
 , Salary NUMBER(8)
 , Hire_Date CHAR(10)
 , Manager_Id NUMBER(6)
 , Department_Id NUMBER(4)
)
 NOLOGGING
 PARALLEL;

-- DML
INSERT ALL
 INTO Employees VALUES(100, 'David', 'OConnell', 4800, '2010/01/01', null, 10)
 INTO Employees VALUES(101, 'Susan', 'Grant', 6500, '2010/12/31', 100, 10)
 INTO Employees VALUES(200, 'Jennifer', 'Whalen', 4400, '2010/01/01', null, 20)
 INTO Employees VALUES(201, 'Bruce', 'Hartstein', 6000, '2010/12/31', 200, 20)
 INTO Employees VALUES(202, 'Pat', 'Fay', 6000, '2010/12/31', 200, 20)
SELECT * FROM DUAL;


Create a Spring Boot Web Project


Let’s start with creating a Spring Boot project. 

Click New button, choose Spring Starter Project, as shown in the screenshot below.



Click Next.

Enter a preferred project name. Click Next. 

On the dependencies tab, check Oracle Driver, Spring Data JDBC, Spring Web,  and Thymeleaf. These dependencies will be added into the pom.xml file automatically by the wizard.



Click Finish.



Dependencies


If you prefer to manually manage the dependencies, make sure the following list of libraries is included in pom.xml. 


SpringMVC project/pom.xml
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
	<dependency>
		<groupId>com.oracle.database.jdbc</groupId>
		<artifactId>ojdbc11</artifactId>
		<scope>runtime</scope>
	</dependency>


Configure a Data Source


We’ll let Spring Boot to configure the default data source, so we only put down the connection strings in the application property file. This is what they look like.


SpringMVC project/application.properties
spring.config.activate.on-profile=dev
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xepdb1
spring.datasource.username=user
spring.datasource.password=password


Data class


The data retrieved from the Employees table is stored in employee POJOs in the memory. Here is how the class is defined.


SpringMVC project/EmpDto.java
public class EmpDto {
	private Integer employee_id;
	private String first_name;
	private String last_name;
	private Double salary;
	private String hire_date;	
	private Integer manager_id;
	private Long department_id;
	/*
	 * Constructors, Getters, Setters etc.
	 */
	…
}


Controller


A HTTP request eventually comes down to a handler method, where it is processed. The following annotations are used to declare controllers and handler methods.

  • @Controller – declaring a controller
  • @GetMapping – mapping a HTTP GET request to a method (@PostMapping, @PutMapping, @PatchMapping and @DeleteMapping work for the other requests.)


Here’s how the controller and the handler method are defined in this example.

The handler returns a ModelAndView object to the client bearing a logical view name, ”employees”, and an object(Model), a collection of employee POJOs, which is identified by “employees”.


SpringMVC project/EmpController.java
@Controller
public class EmpController{
	
	@Autowired
	private EmpService empService;
		
	@GetMapping(AppConstant.PATH_EMP_FINDALL)
	public ModelAndView findAllEmps(ModelAndView mav) {
		List<EmpDto> emplist = empService.findAll();
		
		mav.setViewName(AppConstant.VIEW_EMPLOYEE);
		mav.addObject("employees", emplist);
	    
		return mav;
	}
}

The constant strings used in the preceding code block are defined as follows.


SpringMVC project/AppConstant.java
public final class AppConstant {
	public static final String VIEW_EMPLOYEE = "employees";
	public static final String PATH_EMP_FINDALL = "/findallemps";
}


Model


Model encapsulates the data. Data doesn’t come from nowhere; therefore, we’d look at it in a broader context here. How the data is processed behind, e.g., business logics involved, is treated as a part of model, conceptually. 

In this project, there is  one more layer in between the service layer and the DAO. For the purpose of demo, we bypass the business logic layer and access the DAO directly from the EmpService bean, so, we can make the program easier to read. 


Service Bean:


SpringMVC project/EmpService.java
@Service
public class EmpService{
	
	@Autowired
	EmpDao empDao;
	
	public List<EmpDto> findAll() {
		return empDao.findAll();
	}
}

DAO:

JDBC is employed to access data in the database. As Spring Boot takes care of the configuration, we can autowire a JdbcTemplate and use it in our DAO.


SpringMVC project/EmpDaoImpl.java
@Repository
public class EmpDaoImpl implements EmpDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	public List<EmpDto> findAll(){
		String sql = "SELECT employee_id, first_name, last_name, salary, "
			+ "hire_date, manager_id, department_id "
			+ "FROM employees "
			+ "ORDER BY employee_id";
        
		return jdbcTemplate.query(sql, new RowMapper<EmpDto>() {
			@Override
			public EmpDto mapRow(ResultSet rs, int rowNum) throws SQLException {
				EmpDto empDto = new EmpDto(
					rs.getInt("employee_id"),
					rs.getString("first_name"),
					rs.getString("last_name"),
					rs.getDouble("salary"),
					rs.getString("hire_date"),
					rs.getInt("manager_id"),
					rs.getLong("department_id"));

				return empDto;
			}
		});	
	};
}


View


The view, employees.html, is the front page presented before users. 

Thymeleaf helps us render the employee list to the view. We include it into the view.

	<html xmlns:th=http://www.thymeleaf.org>

The object returned by the servlet can be accessed with the expression below.

	${employees}

This is what the view’s html looks like.


SpringMVC project/employees.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Spring Web App</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" 
    	rel="stylesheet">
</head>
<body>
	<div class='container outer' style="width: 70%">
	    <h2>Employee Console</h2>
	    <br/>
	    
	    <div class="row">
	    		<table class="table table-hover">
		    		<thead>
				    <tr class="table-primary">
				    	<th scope="col">Employee ID</th>
				    	<th scope="col">First Name</th>
				    	<th scope="col">Last Name</th>
				    	<th scope="col">Salary</th>
				    	<th scope="col">Hire Date</th>
				    	<th scope="col">Manager ID</th>
				    	<th scope="col">Department ID</th>
				    </tr>
			    </thead>
			    <tbody>
				    <tr th:each="employee : ${employees}">
				        <td th:text="${employee.employee_id}">200</td>
				        <td th:text="${employee.first_name}">Susan</td>
				        <td th:text="${employee.last_name}">Grant</td>
				        <td th:text="${employee.salary}">6500</td>
				        <td th:text="${employee.hire_date}">2010/01/01</td>
				        <td th:text="${employee.manager_id}">100</td>
				        <td th:text="${employee.department_id}">10</td>
				    </tr>
			    </tbody>
		    </table>
	    </div>
	    <br/>
	    <div class="row">
	    		<div class="col"></div>
	    		<div class="col"></div>
	    		<div class="col"></div>
	    		<div class="col"></div>
	    		<div class="col"></div>
	    		<div class="col">
	    		<a href="/" class="btn btn-primary" type="button" 
	    			style="width:180px">Back</a>
	    		</div>
	    </div>
	</div>
</body>
</html>


Run the App


We still need an entrance to launch the view. Spring Boot looks for index.html under “static” folder at execution,  so we can embed a link in it. A very simple version can be like this.


index.html
<!DOCTYPE HTML>
<html>
<head> 
<title>Spring Web App</title> 
</head>
<body>
	<h2>Click the link below</h2>
	<br/>
	<div>
	<a href="/findallemps">Show Employees</a>
	</div>
</body>
</html>

After we got index.html in place, launch the web app from Eclipse. 

Enter “localhost:8080” in the browser.

When the index page shows up, click the link. You’ll be directed to the view.




Recap


As a classic pattern for web applications, MVC model has thrived in web app development since it was introduced. It still prevails in this domain. We didn’t delve into the mechanism behind the scene in the demo, instead, our focus was to build a lightweight web app by taking advantage of Spring Boot’s capability, hence we can get a glimpse of the simplicity and productivity that Spring Boot provides.



Reference


[Tips] Spring Boot – Access Data with JDBC



[Tips] Spring Boot - Build a Web Client to Consume REST APIs

Last updated on:

 

In this example, we’re going to build a web client to call the REST APIs built in the previous blog, [Tips] Spring Boot - Create REST APIs with WebFlux. We’ll call it API provider going forward. 

Below are the URIs:

  • http://localhost:8080/rest/findalldpts - get a collection of departments,

  • http://localhost:8080/rest/findbyid/{department_id} - get the department identified by department_id.

The example projects are available on GitHub.


Prerequisite


Spring Tool Suite is required for this example.

Assume Eclipse is the IDE for development. The tool suite is findable in Eclipse Marketplace.



Create a Spring Boot Project


Go to File menu, click New, select Other, choose Spring Starter Project under Spring Boot.


Check Spring Reactive Web on New Spring Starter Project Dependencies page.

Click Finish button.



This will add WebFlux dependency to the pom.xml file.


SpringWebClient project/pom.xml
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-webflux</artifactId>
	</dependency>



Data Class


Departments are stored in department POJOs in the app. Here is how the class defined.

SpringWebClient project/DptDto.java
public class DptDto {
	private Long department_id;
	private String department_name;
	private Integer manager_id;
	/*
	 * Constructors, Getters, Setters etc.
	 */
}


Configuration


As the API provider uses the default port number, this web client will take 9090 when it is launched.

The host hosting REST APIs is defined as a property, which is extracted by the program when creating the SpringWebClient bean. 


SpringWebClient project/application.properties
server.port=9090
rest.host=http://localhost:8080

Instead of hard-coding the API’s paths in the calling methods, we declare a few constants, which are concatenated to form the URIs.


SpringWebClient project/AppConstant.java
public final class AppConstant {	
	public static final String REST_ROOT = "/rest";
	public static final String API_DPT_FINDALL = "/findalldpts";
	public static final String API_DPT_FINDBYID = "/finddptbyid/";
}

WebClient is used to access the endpoint. 

We expect findAllDpts() to get us a collection of items, findById() to give us one item. In the meanwhile, we also need to take into account an exceptional case, that is, no item is found.

We use onStatus() to deal with this case. If NOT_FOUND status is received, throw an exception.

.onStatus(status -> status.equals(HttpStatus.NOT_FOUND), clientResponse -> 
	Mono.error(new Exception("Departments not found")))

Subsequently, at onErrorResume(), return an empty Flux or an empty Mono. As a consequence, you won’t get an error when you block for the content.

.onErrorResume(Exception.class, e -> {return Flux.empty();})

SpringWebClient project/SpringWebClient.java
@Component
public class SpringWebClient {
	private final WebClient webClient;

	public SpringWebClient(
			WebClient.Builder builder, 
			@Value("${rest.host}") String restHost) {
		this.webClient = builder.baseUrl(restHost).build();
	}

	public Flux<DptDto> findAllDpts() {	
		String uri = AppConstant.REST_ROOT + AppConstant.API_DPT_FINDALL;
		return this.webClient
				.get()
				.uri(uri)
				.accept(MediaType.APPLICATION_JSON)
				.retrieve()
				.onStatus(status -> status.equals(HttpStatus.NOT_FOUND), 
					clientResponse -> Mono.error(
						new Exception("Departments not found")))
				.bodyToFlux(DptDto.class)
				.onErrorResume(Exception.class, 
					e -> {return Flux.empty();});
	}
	
	public Mono<DptDto> findById(Long department_id) {
		String uri = AppConstant.REST_ROOT + AppConstant.API_DPT_FINDBYID 
			+ Long.toString(department_id);
		
		return this.webClient
				.get()
				.uri(uri)
				.accept(MediaType.APPLICATION_JSON)
				.retrieve()
				.onStatus(status -> status.equals(HttpStatus.NOT_FOUND), 
					clientResponse -> Mono.error(
						new Exception("Department not found")))
				.bodyToMono(DptDto.class)
				.onErrorResume(Exception.class, 
					e -> {return Mono.empty();});
	}
}


Get These to Work


In the main() method, we use the application context to get the SpringWebClient bean. For a Mono object or a Flux object, we call block() to get the items received from the API provider.

You might have noticed, there is a slight difference between Mono and Flux when calling block() to get the items. For Flux, because a list of elements is expected, we call collectList() before block.


SpringWebClient project/SpringWebClientApplication.java
@SpringBootApplication
public class SpringWebClientApplication {

	public static void main(String[] args) {
		ConfigurableApplicationContext context 
			= SpringApplication.run(SpringWebClientApplication.class, args);
		
		SpringWebClient webClient = context.getBean(SpringWebClient.class);

		System.out.println(">> findById " + webClient.findById((long)30).block());
		
		List<DptDto> dptList = webClient.findAllDpts().collectList().block();
		dptList.forEach(dpt -> System.out.println(
			">> findAllDpts = " + dpt.toString()));
	}
}


Test the App


Launch the API provider first. When it is running, start this web client app that we just built. On the console, you'll see the output like this.


>> findById Department[department_id=30, department_name='Compliance', manager_id=300]
>> findAllDpts = Department[department_id=10, department_name='Administration', 
									manager_id=100]
>> findAllDpts = Department[department_id=20, department_name='Marketing', manager_id=200]
>> findAllDpts = Department[department_id=30, department_name='Compliance', manager_id=300]
>> findAllDpts = Department[department_id=40, department_name='Channel', manager_id=400]
>> findAllDpts = Department[department_id=99, department_name='Dummy', manager_id=0]


Recap


Non-blocking WebClient class is used to consume the REST APIs.

We also demonstrated the difference between handling Mono and Flux.

How to handle the case that no item is found was another point presented in the example.



Reference




AI Tools - AntiGravity・Kiroを使ってみよう

Last updated on: AIの開発は加速しています。産業への応用も繰り広がり、手軽に使えるようになりました。システム開発においては、具体的な技術には差異がありますが、パターン化しやすい特徴があり、AIの適応性が優れているとも言えるでしょう。ここでは、AIツールを使ってア...