架構圖
Android App (Retrofit/OkHttp)
│ HTTP/JSON
▼
Nginx (反向代理、SSL終止)
│
▼
Spring Boot / Servlet (Controller → Service → Repository)
│ JDBC / JPA
▼
MySQL / PostgreSQL
Android 端關鍵代碼
// Retrofit 接口
public interface ApiService {
@POST("order/grab")
Call> grabOrder(@Body GrabRequest request);
}
// 調用
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService api = retrofit.create(ApiService.class);
api.grabOrder(new GrabRequest(orderId)).enqueue(new Callback>() {
@Override public void onResponse(Call> call, Response> resp) {
if (resp.isSuccessful() && resp.body().isSuccess()) {
showBuyerInfo(resp.body().getData().getBuyer());
}
}
@Override public void onFailure(Call> call, Throwable t) { ... }
});
服務端關鍵代碼
@RestController
@RequestMapping("/order")
public class OrderController {
@PostMapping("/grab")
public BaseResponse grab(@RequestBody GrabRequest req) {
Order order = orderService.grab(req.getOrderId(), getCurrentUserId());
return BaseResponse.success(order);
}
}
優勢
- 跨語言、跨平台、防火牆友好。
- 成熟生態:Retrofit/OkHttp、Spring Boot、Swagger 文檔自動生成。
- 支持 HTTPS、OAuth2/JWT 鑒權、限流、熔斷等企業級特性。