Headless CMS for Java
Hygraph is the ideal Headless CMS for Java websites and applications. Read further to learn how our API-first CMS allows you to add components to your Java apps in minutes and enable your website's content to be managed from a powerful CMS.
Step #1 - Construct your query and fetch the data from Hygraph
To fetch data from a remote server, you would typically use a library like Apollo Android
, graphql-java-client
, or any HTTP client to send the GraphQL queries as POST requests. You can use a library like OkHttp
to make an HTTP request to a GraphQL API. On the right is a code example that demonstrates how to perform a GraphQL query using OkHttp
to send the request to Hygraph's GraphQL endpoint.
The above code assumes you've added the OkHttp
dependency to your project. If you're using Maven, you'd include it like this in your pom.xml
.
import okhttp3.MediaType;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;import okhttp3.Response;import java.io.IOException;public class GraphQLClientExample {public static void main(String[] args) throws IOException {OkHttpClient client = new OkHttpClient();String graphqlQuery = "{\"query\":\"{ hello }\"}";String url = "https://api-<region>.hygraph.com/v2/<some hash>/master";RequestBody body = RequestBody.create(graphqlQuery,MediaType.get("application/json; charset=utf-8"));Request request = new Request.Builder().url(url).post(body).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);// Body is never null for .execute(), no need to check for null hereSystem.out.println(response.body().string());}}}
Step #2 - Perform mutations in Java to store data in a headless CMS
Hygraph's GraphQL API also allows you to store the data with GraphQL mutations.
The graphqlMutation
variable holds the JSON payload with your mutation and the associated variables. This mutation would create a new product with the provided details, and the response should contain the ID, name, and description of the newly created product.
import okhttp3.MediaType;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.RequestBody;import okhttp3.Response;import java.io.IOException;public class GraphQLMutationExample {public static void main(String[] args) throws IOException {OkHttpClient client = new OkHttpClient();String graphqlMutation = "{"+ "\"query\":\"mutation CreateProduct($input: ProductInput!) {"+ " createProduct(input: $input) {"+ " id"+ " name"+ " description"+ " }"+ "}\","+ "\"variables\":{\"input\":{"+ " \"name\":\"New Product\","+ " \"description\":\"This is a new product.\","+ " \"slug\":\"new-product\","+ " \"availability\":\"IN_STOCK\","+ " \"imageUrl\":\"http://example.com/product.jpg\""+ "}}"+ "}";String url = "https://management.hygraph.com/graphql";RequestBody body = RequestBody.create(graphqlMutation,MediaType.get("application/json; charset=utf-8"));Request request = new Request.Builder().url(url).post(body).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);System.out.println(response.body().string());}}}
Start building with Java
We made it really easy to set up your project in Hygraph and use our GraphQL API within your Java project.
Quickstart
Check out our docs to see how you can quickly set up your Hygraph project and enable the content API for your Java website or app.
Learn GraphQL
Hygraph is GraphQL-native Headless CMS offers precise data retrieval, minimizing over-fetching and optimizing efficiency.
Examples
Look at some of the example projects to see Hygraph in action.
Why Hygraph
Choosing Hygraph for your Java project
Using a GraphQL-native headless CMS with a Java app streamlines data fetching for developers, allowing for precise and efficient data queries. This leads to faster performance and easier integration.
Content editors benefit from the CMS's independence from the app's frontend, facilitating a flexible content management process. They can update and deliver content across platforms without technical constraints, while GraphQL ensures content queries remain current, reducing publishing errors.
Developer Experience
We try to be the most un-opinionated CMS on the market with a wide collection of open source example projects to get you started.
Headless CMS
As a headless CMS (i.e. API based content management), you can be as modular and flexible as you need. We even support multiplatform content management.
Management API
Hygraph boasts a flexible and powerful management API to manage your content and schema, as well as a blazing fast content API.