title: Unable to parse JWK in Java
body: <p>I implemented a rest authorization server that returns the public-key for a given keyId in the JWK format using the <code>com.nimbusds:nimbus-jose-jwt:9.13</code> package. The code looks something like this:</p>
  <pre><code>@RequestMapping(value = &quot;/oauth2&quot;, produces = APPLICATION_JSON_VALUE)
  public interface Rest {
  ...
      @GetMapping(&quot;/public-key/{keyId}&quot;)
      @Operation(summary = &quot;Return the public key corresponding to the key id&quot;)
      JWK getPublicKey(@PathVariable String keyId);
  }
  
  public class RestController implements Rest {
  .....
   public JWK getPublicKey(String keyId) {
           byte[] publicKeyBytes = ....
           RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
           JWK jwk = new RSAKey.Builder(publicKey)
                  .keyID(keyId)
                  .algorithm(new Algorithm(publicKey.getAlgorithm()))
                  .keyUse(KeyUse.SIGNATURE)
                  .build();
           return jwk;
      }
  }
  </code></pre>
  <p>This code returns a JWK key in the following format:</p>
  <pre><code>{
      &quot;keyStore&quot;: null,
      &quot;private&quot;: false,
      &quot;publicExponent&quot;: {},
      &quot;modulus&quot;: {},
      &quot;firstPrimeFactor&quot;: null,
      &quot;secondPrimeFactor&quot;: null,
      &quot;firstFactorCRTExponent&quot;: null,
      &quot;secondFactorCRTExponent&quot;: null,
      &quot;firstCRTCoefficient&quot;: null,
      &quot;otherPrimes&quot;: [],
      &quot;requiredParams&quot;: {
          &quot;e&quot;: &quot;some-valid-exponent&quot;,
          &quot;kty&quot;: &quot;RSA&quot;,
          &quot;n&quot;: &quot;some-valid-modulus&quot;
      },
      &quot;privateExponent&quot;: null,
      &quot;x509CertChain&quot;: null,
      &quot;algorithm&quot;: {
          &quot;name&quot;: &quot;RSA&quot;,
          &quot;requirement&quot;: null
      },
      &quot;keyOperations&quot;: null,
      &quot;keyID&quot;: &quot;some-valid-key-id&quot;,
      &quot;x509CertURL&quot;: null,
      &quot;x509CertThumbprint&quot;: null,
      &quot;x509CertSHA256Thumbprint&quot;: null,
      &quot;parsedX509CertChain&quot;: null,
      &quot;keyUse&quot;: {
          &quot;value&quot;: &quot;sig&quot;
      },
      &quot;keyType&quot;: {
          &quot;value&quot;: &quot;RSA&quot;,
          &quot;requirement&quot;: &quot;REQUIRED&quot;
      }
  }
  </code></pre>
  <p>On the client side (java), I try to parse the jwk with the following code:</p>
  <pre><code>public JWK getPublicKey(String keyId) {
   String json = restTemplate.getForObject(publicUrl + &quot;/oauth2/public-key/&quot; + keyId, String.class);
          try {
              return JWK.parse(json);
          } catch (ParseException e) {
              log.error(&quot;Unable to parse JWK&quot;, e);
              return null;
          }
  }
  </code></pre>
  <p>However, the client is unable to parse the key since <code>parse</code> throws an exception (<code>Missing parameter &quot;kty&quot;</code>). I see that <code>JWK.parse</code> requires a <code>kty</code> key in main JWT josn body, while the default serialization of <code>JWK</code> embeds the <code>kty</code> key within <code>requiredParams</code> key. When I try <code>jwk.toString()</code>, I do see the <code>kty</code> key in the main json body.</p>
  <p>Why doesn't serialization/deserialization of the native JWK object work in a straight-forward manner? What would be the best way to fix this without implementing a custom jwt structure or a serializer/deserializer?</p>
  <p><strong>Update 1</strong>: This code will work if we change the return type from <code>JWK</code> to <code>Map&lt;String, Object&gt;</code> or <code>String</code> and handle deserialization on the client-side. However, it would be better if the package natively does the (de)serialization for us.</p>