Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now
Build the future of communications.
Start building for free

Java posts

  • By Matthew Gilliard
    What's new for developers in Java 15? Title: What's new for developers in Java 15?

    [header image credit: NASA Astronomy Picture Of the Day, Sep 15 2020]

    Happy Java 15 release day! This release marks three years since the start of the six-monthly release schedule and I am still appreciating the speed of new features and the insight it's giving into what's coming up in Java's future.

    In Java 15 there are improvements to tooling, security, cross-platform consistency, efficiency and performance. You can read the full release notes and the list of major features, which are designated a Java Enhancement Proposal (JEP) number. In this post I've selected a few features which I think are interesting for developers.

    The features I've selected are a mix of fully-released and "preview" features (more on these later).

    How to use these features

    You can download Java 15 from https://jdk.java.net/15/ - downstream builds will be available from places like AdoptOpenJDK soon after. You can also install it …

    Read More
  • By Matthew Gilliard
    Securing your Twilio webhooks in Java Header image: Securing your Twilio webhooks in Java

    Using webhooks is a common and powerful way to configure your Twilio phone numbers to respond to incoming phone calls or SMS (or faxes). You provide a URL and Twilio makes an HTTP request to that URL when a call or message comes in to find out how to respond.

    Diagram of the flow: Someone sends SMS, Twilio makes an HTTP request to your app, the response is passed back to Twilio which sends an SMS reply.

    One implication of this is that the URL you provide has to be accessible from the internet, so when you're building your web app to handle the webhooks, how can you know for sure that the requests are really coming from Twilio and not malicious or opportunistic internet scoundrels?

    The answer is that all valid webhook requests from Twilio are signed in a header called X-Twilio-Signature. The algorithm used to create the signature is described in detail in our docs.  Briefly, the ingredients of the signature are:

    • The details of the request
    • The webhook URL as configured …
    Read More
  • By Emily Joy Krohn
    How to Improve Your Java Workflow with GitHub API header - How to Improve Your Java Workflow with GitHub API

    By now, you have probably heard about the GitHub API. Or you know what GitHub is and you know what an API is. Or you just Googled it, that’s okay too.

    From automating GitHub processes to just being a command line fanatic, the GitHub API can be used in many different ways. By taking a brief look at the documentation, you can see the GitHub API can do just about everything and more.

    Stay tuned to find out many cool features you probably didn’t know the GitHub API has and make your automation process a walk in the park.

    Read to the end for a quick and easy tutorial!

    The Convenience of GitHub API

    As many enterprises use GitHub in their everyday work, it may not come as a surprise to learn the base URL can be different from those which are public repositories.

    The most difficult part in …

    Read More
  • By Matthew Gilliard
    How to build a CLI app in Java using jbang and picocli Title card: How to build a CLI app in Java using jbang and picocli

    Traditionally Java applications have been used for long-running processes - web application servers can run for days or weeks at a time. The JVM handles this well: Garbage Collection is efficient over huge amounts of memory, and Profile-Guided Optimization can make your code faster the longer it runs.

    However, it’s perfectly possible to write short-lived apps too, and in this post I’ll show how to build a CLI app whose total runtime is just a couple of seconds. You can build sophisticated CLI tools in Java for data processing, connection to databases, fetching data from the web, or taking advantage of any of the Java libraries that you're used to.

    I’ll use jbang for packaging and running the app, and picocli to handle argument parsing and output.  The app will send an SMS using Twilio’s Messaging API, in a single Java source file less than 100 lines long. …

    Read More
  • By Matthew Gilliard
    5 ways to make HTTP requests in Java 5 ways to make HTTP requests in Java

    [header image credit: Iron in the Butterfly Nebula, NASA Astronomy Picture of the Day July 21 2020 (modified)]

    Making HTTP requests is a core feature of modern programming, and is often one of the first things you want to do when learning a new programming language. For Java programmers there are many ways to do it - core libraries in the JDK and third-party libraries. This post will introduce you to the Java HTTP clients that I reach for. If you use other ones, that’s great! Let me know about it. In this post I’ll cover:

    Core Java:
    • HttpURLConnection
    • HttpClient
    Popular Libraries:
    • ApacheHttpClient
    • OkHttp
    • Retrofit

    I’ll use the Astronomy Picture of the Day API from the NASA APIs for the code samples, and the code is all on GitHub in a project based on Java 11.

    Core Java APIs for making Java http requests

    Since Java 1.1 there has …

    Read More
  • By Matthew Gilliard
    Take control of incoming calls with Twilio, Java and Google Sheets Take Control of Incoming Calls with Twilio, Java and Google Sheets

    We understand how phone numbers work, don’t we? Someone calls your number and your phone rings. But wouldn't it be nice to have more control - to screen incoming calls and treat them differently depending on who is calling?

    Gif of a man answering a phone call

     

    In this post I’ll show you how to set up a Twilio phone number to handle incoming calls using a Spring Boot app. Your app will look up the caller in a Google Sheet to decide what to do. There are options for blocking calls or forwarding them to other numbers. Any changes made to the Google Sheet will take effect immediately. The sheet looks like this:

    Screenshot of a Google Sheet with Columns for "Caller's Number" and "Action". Action is either "Block" (with a message) or "Forward" (to another phone number)

    Once you’ve set this up you can hand out your Twilio phone number freely, knowing that you can easily block unwanted callers, or redirect people to any other number depending on who they are.

    To set this up you will need:

    • A Google account …
    Read More
  • By Matthew Gilliard
    Three ways to use Jackson for JSON in Java Three ways to use Jackson for JSON in Java

    If you’re working in a statically-typed language like Java then dealing with JSON can be tricky. JSON doesn’t have type definitions and is lacking some features which we would like - there’s only strings, numbers, booleans and null, so to store other types (like dates or times) we’re forced to use a string-based convention. Despite its shortcomings, JSON is the most common format for APIs on the web so we need a way to work with it in Java.

    Jackson is one of the most popular Java JSON libraries, and is the one I use most frequently. In this post I’ll pick a fairly complex JSON document and three queries which I want to make using Jackson. I’ll compare three different approaches:

    1. Tree model
    2. Data binding
    3. Path queries

    All the code used in this post is in this repository. It’ll work with Java 8 onwards.

    Other Java Libraries …

    Read More
  • By Matthew Gilliard
    Two ways to use Gson for JSON in Java Two ways to use Gson for JSON in Java

    If you’re working in a statically-typed language like Java then dealing with JSON can be tricky. JSON doesn’t have type definitions and is lacking some features which we would like - there’s only strings, numbers, booleans and null, so to store other types (like dates or times) we’re forced to use a string-based convention. Despite its shortcomings, JSON is the most common format for APIs on the web so we need a way to work with it in Java.

    Gson is one of the most popular Java JSON libraries. In this post I’ll pick a fairly complex JSON document and three queries which I want to make using Gson. I’ll compare two different approaches:

    1. Tree model
    2. Data binding

    All the code used in this post is in this repository. It’ll work with Java 8 onwards.

    Other Java Libraries for working with JSON

    The most popular Java libraries for …

    Read More
  • By Matthew Gilliard
    Open and Click Tracking for Email using Java and Twilio SendGrid Open and Click Tracking for Email using Java and Twilio SendGrid

    I covered sending emails from Java using the Twilio SendGrid API before, but for important emails it can be vital to know whether recipients have opened your mail, and whether they have clicked on the links in it. You can do this using the SendGrid Event Webhook, which works by sending HTTP requests to a URL you provide. In this post I’ll show how to configure this with Twilio SendGrid and build a Java web application to handle the webhooks and record your recipients’ activity.

    Before you start

    You will need

    • Java - version 8 or newer. I like to use SDKMAN! to manage Java installations
    • Ngrok
    • A Twilio SendGrid account - sign up here if you don’t have one already.

    Creating the application

    If you prefer to skip the coding and instead try things out you can find the code for this application on GitHub, and …

    Read More
  • By Matthew Gilliard
    Validating phone numbers in a Spring Boot app using the Twilio Lookup API Validating phone numbers in a Spring Boot app using the Twilio Lookup API

    Many user registration flows these days require a user to provide a phone number, either for two-factor authentication or to provide notifications and updates.

    To check that the number provided by your user is valid, you might reach for a regular expression, but that won’t be reliable - phone numbers are complicated, and it won’t tell you if the number provided is actually reachable. There are multiple levels of validation possible - in this post I’ll show how to use the Twilio Lookup API to check if a real phone number is real.

    The demo application will use Spring Boot and I will show how to use the Bean Validation framework to create a @ValidPhoneNumber annotation that can be used like you would @NotNull.

    Starting out

    What you will need before you start

    • A Twilio account. Sign up using this link and you can get an extra $10 …
    Read More
  • Newer
    Older
    Sign up and start building
    Not ready yet? Talk to an expert.