I've been struggling with a JSON parsing error where my application is using the Spring to send and receive messages from a RESTful Web Service. It's pretty straight forward: I've annotated my object properties to match up with the appropriate JSON keys, Spring takes my POJO and turns it into a JSON string sends the request along with the JSON as the body to the HTTPS endpoint, et voilĂ !
The Problem
The problem comes in when something goes wrong with the request/response. Because the Spring obfuscates the actual request/response content, debugging it means you need to take a look at the traffic being sent over the wire. Since we're using a good RESTful service, the connection is done through HTTPS, meaning it's encrypted with a certificate that we don't have.
On top of that, it appears that Fiddler doesn't automatically capture Java HTTP traffic automatically, so that's a thing too.
After some internet sleuthing, I put together a solution that I wanted to share with you all, and so that I don't forget how to do it myself.
Setup
- Downlaod and Install Fiddler. I used Fiddler4, because I'm awesome.
- Run it and make sure it's capturing HTTP traffic
- Open Tools --> Fiddler Options --> Connections Tab and take note of the "Fiddler listens on port" value. It's likely 8888, but best to be sure.
- In the same window select HTTPS Tab and make sure sure that the following options are checked:
- Capture HTTPS CONNECTS
- Decrypt HTTPS traffic (...from all processes)
- Read, and if you're alright with it, install the certificate.
- On the HTTPS tab, click the Export Root Certificate to Desktop and click OK.
Generating a Keystore
- Open a command line terminal as an administrator
- Run the keytool for the JDK your application is using:
1 |
|
- Enter a password and remember it
- Your keystore is created as a file named "FiddlerKeystore*. Take note of where it is located on your machine.
Configuring Eclipse
NOTE: You are not required to use Eclipse for this, but it seems to be the popular way of writing Java code.
- Open your project and go to Run --> Run Configurations
- Select the Run Configuration you want to use where you'll capture the HTTPS traffic.
- Select the Arguments tab
- Add the following to the VMargs textbox:
1 |
|
- Click the Apply button
- Click the Run button to try it out
Tada! You're done, and you should now be able to run your code and see the HTTP request and response, completely.
Alternative Solution --- Configuring Your Code
Add the following lines to the application that you want to capture the HTTPS traffic.
1 |
|
-- Thanks for playing. ~ DW