Webhook handler in Java server

1,136 views
Skip to first unread message

Gideon Grossman

unread,
Jul 25, 2019, 11:32:26 AM7/25/19
to Stripe API Discussion
I keep receiving this error message when I attempt to send a webhook to my server.
a <html><body><h1>502 Bad Gateway</h1>
The server returned an invalid or incomplete response.
</body></html>
The full url for my api is http://localhost:8081/api.
I am using ngrok to tunnel this url to the internet. The resulting urls are https://75bb9fa8.ngrok.io and http://75bb9fa8.ngrok.io
In the Stripe dashboard, I created a webhook and set the endpoint to https://75bb9fa8.ngrok.io/api/webhooks/webhook, as this is the full url and path of my sever method that receives webhooks.

Webhook.JPG


Here is my complete webhook receiver class. It is based on https://stripe.com/docs/webhooks/setup...
package org.groundlist.core.services;

import java.util.Optional;

import javax.ws.rs.Path;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonSyntaxException;
import com.stripe.model.Event;
import com.stripe.model.EventDataObjectDeserializer;
import com.stripe.model.StripeObject;
import com.stripe.net.ApiResource;

import spark.Request;
import spark.Response;

@Path("/webhooks")
public final class WebhookService {
private static final Logger LOGGER = LoggerFactory.getLogger(WebhookService.class);

public WebhookService() {
}

// Retrieve webhook endpoint's signing secret
final String endpointSecret = "whsec_4S4Htm9f2qU8llC70kDRP0AkP5Fl6gkx";

// Using the Spark framework (http://sparkjava.com)
@POST
@Path("/webhook")
public Object handle(RequestResponse requestresponse) {
Request request = requestresponse.request;
Response response = requestresponse.response;
String payload = request.body();
Event event = null;

try {
event = ApiResource.GSON.fromJson(payload, Event.class);
} catch (JsonSyntaxException e) {
// Invalid payload
response.status(400);
return "";
}

// Deserialize the nested object inside the event
EventDataObjectDeserializer dataObjectDeserializer = event.getDataObjectDeserializer();
Optional<StripeObject> stripeObject = null;
if (dataObjectDeserializer.getObject().isPresent()) {
stripeObject = dataObjectDeserializer.getObject();
} else {
// Deserialization failed, probably due to an API version mismatch.
// Refer to the Javadoc documentation on `EventDataObjectDeserializer` for
// instructions on how to handle this case, or return an error here.
}

// Handle the event
switch (event.getType()) {
case "customer.created":
System.out.println("customer created webhook");
break;
default:
// Unexpected event type
response.status(400);
return "";
}

response.status(200);
return "";
}

public static class RequestResponse {
Response response;
Request request;
}
}

And for the sake of completeness, here are my server logs...
2019-07-25 03:09:01.997 DESKTOP-2AB6RK0 com.dieselpoint.dieseljax.server.Server$Builder 766  INFO  Initializing server at http://192.168.1.91:8081/ in C:\Users\User\Documents\Work\Dieselpoint\groundlist\groundlist.core\.
2019-07-25 03:09:02.024 DESKTOP-2AB6RK0 org.eclipse.jetty.util.log 793  INFO  Logging initialized @1511ms to org.eclipse.jetty.util.log.Slf4jLog
2019-07-25 03:09:02.366 DESKTOP-2AB6RK0 org.eclipse.jetty.server.Server 1135 INFO  jetty-9.4.12.v20180830; built: 2018-08-30T13:59:14.071Z; git: 27208684755d94a92186989f695db2d7b21ebc51; jvm 1.8.0_191-b12
2019-07-25 03:09:03.619 DESKTOP-2AB6RK0 org.hibernate.validator.internal.util.Version 2388 INFO  HV000001: Hibernate Validator 6.0.11.Final
2019-07-25 03:09:04.038 DESKTOP-2AB6RK0 org.eclipse.jetty.server.handler.ContextHandler 2807 INFO  Started o.e.j.s.ServletContextHandler@402f80f5{/,null,AVAILABLE}
2019-07-25 03:09:04.296 DESKTOP-2AB6RK0 org.eclipse.jetty.server.AbstractConnector 3065 INFO  Started ServerConnector@5c909414{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2019-07-25 03:09:04.297 DESKTOP-2AB6RK0 org.eclipse.jetty.server.Server 3066 INFO  Started @3787ms
2019-07-25 03:09:35.763 DESKTOP-2AB6RK0 com.dieselpoint.dieseljax.server.ExceptionMappers$OtherExceptionMapper 34532 ERROR java.lang.NullPointerException
java.lang.NullPointerException: null
at org.groundlist.core.services.WebhookService.handle(WebhookService.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ObjectOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:192)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:679)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:392)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:873)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:542)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:703)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:505)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:370)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:267)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
at java.lang.Thread.run(Unknown Source)
2019-07-25 03:09:35.911 DESKTOP-2AB6RK0 org.glassfish.jersey.server.ServerRuntime$Responder 34680 ERROR Error occurred when processing a response created from an already mapped exception.
2019-07-25 03:09:35.916 DESKTOP-2AB6RK0 org.eclipse.jetty.server.HttpChannel 34685 WARN  /api/webhooks/webhook
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: org.eclipse.jetty.util.BufferUtil.remaining([Ljava/nio/ByteBuffer;)J
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:408)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:873)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:542)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:703)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:505)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:370)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:267)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
at java.lang.Thread.run(Unknown Source)
Caused by: org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: org.eclipse.jetty.util.BufferUtil.remaining([Ljava/nio/ByteBuffer;)J
at org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:254)
at org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:236)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:436)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:261)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:679)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:392)
... 26 common frames omitted
Caused by: java.lang.NoSuchMethodError: org.eclipse.jetty.util.BufferUtil.remaining([Ljava/nio/ByteBuffer;)J
at org.eclipse.jetty.io.WriteFlusher.flush(WriteFlusher.java:392)
at org.eclipse.jetty.io.WriteFlusher.write(WriteFlusher.java:277)
at org.eclipse.jetty.io.AbstractEndPoint.write(AbstractEndPoint.java:380)
at org.eclipse.jetty.server.HttpConnection$SendCallback.process(HttpConnection.java:814)
at org.eclipse.jetty.util.IteratingCallback.processing(IteratingCallback.java:241)
at org.eclipse.jetty.util.IteratingCallback.iterate(IteratingCallback.java:224)
at org.eclipse.jetty.server.HttpConnection.send(HttpConnection.java:550)
at org.eclipse.jetty.server.HttpChannel.sendResponse(HttpChannel.java:845)
at org.eclipse.jetty.server.HttpChannel.write(HttpChannel.java:921)
at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:249)
at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:225)
at org.eclipse.jetty.server.HttpOutput.write(HttpOutput.java:550)
at org.glassfish.jersey.servlet.internal.ResponseWriter$NonCloseableOutputStreamWrapper.write(ResponseWriter.java:301)
at java.io.ByteArrayOutputStream.writeTo(Unknown Source)
at org.glassfish.jersey.message.internal.CommittingOutputStream.flushBuffer(CommittingOutputStream.java:278)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:232)
at org.glassfish.jersey.message.internal.CommittingOutputStream.close(CommittingOutputStream.java:247)
at org.glassfish.jersey.message.internal.OutboundMessageContext.close(OutboundMessageContext.java:824)
at org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:389)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:710)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:371)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:417)
... 36 common frames omitted


 
What am I doing incorrectly?
Thank you.

Gideon Grossman

unread,
Jul 25, 2019, 12:00:16 PM7/25/19
to Stripe API Discussion
 Perhaps the problem has to do with the parameters of my webhook method. The @POST and @Path annotations require that the method only take a single JSON object as a parameter. However, the webhook might be sending two separate parameters - a request object and a response object. If you think this inconsistency is the source of the problem, then how can I refactor the server code to accommodate the structure of the webhook?

Remi J.

unread,
Jul 25, 2019, 12:31:54 PM7/25/19
to api-d...@lists.stripe.com
Hello Gideon,

I'm having some trouble following this ask and this could take many back and forth. Since this is a public mailing list, everyone subscribed would get all messages.

I would recommend that you reach out to our support team directly either here https://support.stripe.com/contact?email=true or on IRC as discussed yesterday so that you can get some direct help with the code!

Best,
Remi

--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.
Reply all
Reply to author
Forward
0 new messages