如何在 Jersey ws 中 return x509 证书

How to return x509 certificate in Jersey ws

我能够正确生成证书,但无法 return 返回给客户端。

我在从我的 REST 资源 return 返回客户端时收到以下错误:

SEVERE: A message body writer for Java class sun.security.x509.X509CertImpl, and Java type class java.security.cert.X509Certificate, and MIME media type application/x-x509-user-cert was not found

客户端代码是正确的,因为它适用于其他服务。

REST 资源:

@POST
    @XmlElement(name = "data")
    @Path("/,system/newCert")
    @Consumes({ "application/x-www-form-urlencoded" })
    @Produces({ "application/x-x509-user-cert" })
    public X509Certificate newCert(@FormParam("username") String uname,
            @FormParam("name") String CommonName,
            @FormParam("email") String email,
            @FormParam("pictureURL") String pURL,
            @FormParam("spkac") String spkacData) {

        String webId = "https://" + uname + "/profile/card#me";

        BouncyKeygenService keygen = new BouncyKeygenService();

        try {

            keygen.initialize();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Certificate cert = null;

        if (!spkacData.isEmpty()) {
            cert = keygen.createFromSpkac(spkacData);
        }

        cert.setSubjectCommonName(CommonName);
        cert.setSubjectWebID(webId);
        cert.addDurationInDays("36135"); // valid for 99 years
        cert.startEarlier("12"); 

        CertSerialisation certByte = null;
        X509Certificate x509 = null;
        try {
            certByte = cert.getSerialisation();

            ByteArrayOutputStream bout = new ByteArrayOutputStream(
                    certByte.getLength());
            certByte.writeTo(bout);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            x509 = (X509Certificate) cf
                    .generateCertificate(new ByteArrayInputStream(bout
                            .toByteArray()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Date notAfter = x509.getNotAfter();

        return x509;
    }

问题是 Java 中没有 MediaType x509 证书,应该 returned 是什么?

如有任何帮助,我们将不胜感激。

谢谢。

已解决!

供以后参考; return 类型不能是证书,但按照问题评论中的建议将其包装在 MessageBodyWriter 中。

这是对问题中代码的额外修复:

public InputStream newCert(@FormParam("username") String uname,
        @FormParam("name") String CommonName,
        @FormParam("email") String email,
        @FormParam("pictureURL") String pURL,
        @FormParam("spkac") String spkacData) {

  ...

  ByteArrayOutputStream bout = null;
  InputStream is = null;

  ...

  try {...}
  catch {...}

  is = new ByteArrayInputStream (bout.toByteArray());   
  return is;

}