{"record":{"id":"10d42e6eabacd515","repo":"grpc/grpc-java","slug":"use-grpc-newserverbuilderforport-instead","errorCode":null,"errorMessage":"Use Grpc.newServerBuilderForPort() instead","messagePattern":"Use Grpc\\.newServerBuilderForPort\\(\\) instead","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"okhttp/src/main/java/io/grpc/okhttp/OkHttpServerProvider.java","lineNumber":41,"sourceCode":"\n/** Provider for {@link OkHttpServerBuilder} instances. */\n@Internal\npublic final class OkHttpServerProvider extends ServerProvider {\n\n  @Override\n  protected boolean isAvailable() {\n    return true;\n  }\n\n  @Override\n  protected int priority() {\n    // Use a priority less than Netty since builderForPort() always throws.\n    return 4;\n  }\n\n  @Override\n  protected OkHttpServerBuilder builderForPort(int port) {\n    throw new UnsupportedOperationException(\"Use Grpc.newServerBuilderForPort() instead\");\n  }\n\n  @Override\n  protected NewServerBuilderResult newServerBuilderForPort(int port, ServerCredentials creds) {\n    OkHttpServerBuilder.HandshakerSocketFactoryResult result =\n        OkHttpServerBuilder.handshakerSocketFactoryFrom(creds);\n    if (result.error != null) {\n      return NewServerBuilderResult.error(result.error);\n    }\n    return NewServerBuilderResult.serverBuilder(\n        new OkHttpServerBuilder(new InetSocketAddress(port), result.factory));\n  }\n}\n\n","sourceCodeStart":23,"sourceCodeEnd":56,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/okhttp/src/main/java/io/grpc/okhttp/OkHttpServerProvider.java#L23-L56","documentation":"OkHttpServerProvider.builderForPort(int) is the provider SPI hook for credentials-less server builder creation; in the OkHttp provider it is intentionally never implemented and always throws UnsupportedOperationException because all server creation must go through the ServerCredentials-based Grpc.newServerBuilderForPort path.","triggerScenarios":"Calling this protected provider method directly, or an environment where grpc-core's ServerProvider dispatch falls back to the deprecated builderForPort path (e.g. old grpc-core calling ServerProvider.builderForPort(int)).","commonSituations":"Mixed grpc-core/grpc-okhttp versions where core still uses the legacy SPI; tests instantiating OkHttpServerProvider and invoking builderForPort directly.","solutions":["Use Grpc.newServerBuilderForPort(port, creds) instead of provider.builderForPort(port)","Align grpc-core and grpc-okhttp versions (same release train) so the credentials-based SPI is used","Remove code paths that construct servers via ServerProvider.provider().builderForPort"],"exampleFix":"// before\nOkHttpServerBuilder b = provider.builderForPort(8080);\n// after\nOkHttpServerBuilder b = (OkHttpServerBuilder) Grpc.newServerBuilderForPort(8080, InsecureServerCredentials.getInsecureInstance());","handlingStrategy":"validation","validationCode":"// Use the supported API directly\nServerBuilder<?> b = Grpc.newServerBuilderForPort(port, creds);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never invoke provider SPI methods directly; go through Grpc/ServerBuilder entry points","Keep grpc-core and grpc-okhttp on the same version","Search codebase for builderForPort usages when upgrading"],"tags":["grpc","java","spi","unsupported-operation","deprecated-api"],"backgroundTag":"deprecated-api-usage","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}