/
Java REST Sample Code
Java REST Sample Code
Use the following example classes to deserialize the JSON returned by the REST api.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.doforms.rest</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>doforms_rest_client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.29</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
</project>
package com.doforms.rest.client;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@Provider
public class JacksonConfig implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public JacksonConfig() {
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(Include.NON_NULL);
}
public ObjectMapper getContext(Class<?> aClass) {
return objectMapper;
}
}
package com.doforms.rest.client;
import java.time.OffsetDateTime;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class ClientObjects {
public static class Blob {
public String key;
public String id;
public String type;
}
public static class Column {
public String name;
public String caption;
}
public static class Customer {
public String key;
public String code;
public User administrator;
public Boolean branded;
public Integer credits;
public Integer licenses;
public String name;
public Plan plan;
public String status;
}
public static class Choice {
public String text;
public Label label;
}
public static class Device {
public String key;
public String name;
public String carrier;
public OffsetDateTime createTime;
public OffsetDateTime formTime;
public String model;
public String number;
public String os;
public String teamKey;
public OffsetDateTime updateTime;
public String version;
}
public static class Email {
public String[] addresses;
public String message;
}
public static class ErrorResult {
public String code;
public String request;
public String exception;
public String message;
}
public static class Fax {
public String[] addresses;
public String message;
}
public static class Field {
public String name;
public DataType data;
public ControlType type;
// Begin data fields
public Blob blob;
public String comment;
public OffsetDateTime date; // Ignore the time value which will always be 00:00:00Z
public OffsetDateTime datetime;
public Email email;
public Fax fax;
public Long integer;
public Location location;
public Double number;
public String[] strings;
public String text;
public OffsetDateTime time; // Ignore date value which will always be Jan 1 1970
public String value;
// End data fields
public Options options;
public List<Choice> choices;
public List<Field> fields;
public List<Row> rows;
}
public static class Form {
public String key;
public String id;
public String name;
public String displayName;
public OffsetDateTime createTime;
public String createUser;
public OffsetDateTime updateTime;
public String updateUser;
public Double version;
public List<Field> fields;
}
public static class Label {
public String en;
}
public static class Location {
public Double latitude;
public Double longitude;
public Double altitude;
public Double accuracy;
}
public static class Lookup {
public String key;
public String name;
public String description;
public Boolean useForm;
public Boolean useFilter;
public String formKey;
public String projectKey;
public Boolean update;
public Boolean encrypt;
public Long version;
public OffsetDateTime createTime;
public String createUser;
public OffsetDateTime updateTime;
public String updateUser;
public List<Column> columns;
}
public static class Notification {
public String key;
public String customerKey;
public String dispatchKey;
public String submissionKey;
public String webserviceId;
public String event;
public String status;
public String dispatchStatus;
}
public static class NotificationUpdate {
public String status;
public String message;
}
public static class Options {
public Boolean comment;
public Label commentLabel;
public String format;
public Label label;
public Long level;
public String prefix;
public Long rows;
}
public static class Plan {
public String code;
public String name;
}
public static class Project {
public String key;
public String name;
}
public static class Row {
public long index;
public List<Field> fields;
}
public static class Submission {
public String key;
public String id;
public String name;
public String formName;
public String formKey;
public Double formVersion;
public String projectKey;
public Integer offset;
public String timezone;
public OffsetDateTime receiveTime;
public List<Field> fields;
}
public static class Team {
public String key;
public String name;
}
public static class TokenRequest {
public String username;
public String password;
public String account;
public String code;
public String webservice;
}
public static class TokenResult {
public String code;
public String customerCode;
public String customerKey;
public String message;
public String resellerCode;
public String resellerKey;
public String token;
public String userKey;
public String[] accounts;
}
public static class User {
public String key;
public String email;
public String firstName;
public String lastName;
public String workPhone;
}
public enum ControlType {
// @formatter:off
@JsonProperty("action") ACTION,
@JsonProperty("approval") APPROVAL,
@JsonProperty("audio") AUDIO,
@JsonProperty("autonumber") AUTONUMBER,
@JsonProperty("barcode") BARCODE,
@JsonProperty("bluetooth_le") BLUETOOTH_LE,
@JsonProperty("button") BUTTON,
@JsonProperty("button_grid") BUTTON_GRID,
@JsonProperty("calculation") CALCULATION,
@JsonProperty("choose_one") CHOOSE_ONE,
@JsonProperty("choose_multiple") CHOOSE_MULTIPLE,
@JsonProperty("counter") COUNTER,
@JsonProperty("date_time") DATE_TIME,
@JsonProperty("email") EMAIL,
@JsonProperty("fax") FAX,
@JsonProperty("forward") FORWARD,
@JsonProperty("grid") GRID,
@JsonProperty("image") IMAGE,
@JsonProperty("instruction") INSTRUCTION,
@JsonProperty("label") LABEL,
@JsonProperty("location") LOCATION,
@JsonProperty("lookup") LOOKUP,
@JsonProperty("nfc") NFC,
@JsonProperty("numeric") NUMERIC,
@JsonProperty("page") PAGE,
@JsonProperty("payment") PAYMENT,
@JsonProperty("pod") POD,
@JsonProperty("questionnaire") QUESTIONNAIRE,
@JsonProperty("repeat") REPEAT,
@JsonProperty("retrieve") RETRIEVE,
@JsonProperty("schedule") SCHEDULE,
@JsonProperty("score") SCORE,
@JsonProperty("score_summary") SCORE_SUMMARY,
@JsonProperty("signature") SIGNATURE,
@JsonProperty("sketch") SKETCH,
@JsonProperty("table") TABLE,
@JsonProperty("text") TEXT,
@JsonProperty("trends") TRENDS,
@JsonProperty("video") VIDEO
// @formatter:on
}
public enum DataType {
// @formatter:off
@JsonProperty("blob") BLOB,
@JsonProperty("date") DATE,
@JsonProperty("datetime") DATETIME,
@JsonProperty("email") EMAIL,
@JsonProperty("fax") FAX,
@JsonProperty("fields") FIELDS,
@JsonProperty("integer") INTEGER,
@JsonProperty("location") LOCATION,
@JsonProperty("number") NUMBER,
@JsonProperty("rows") ROWS,
@JsonProperty("strings") STRINGS,
@JsonProperty("text") TEXT,
@JsonProperty("time") TIME
// @formatter:on
}
}
package com.doforms.rest.client;
import java.io.IOException;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.doforms.rest.client.ClientObjects.*;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
public class App {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
Client client = ClientBuilder.newClient().register(JacksonConfig.class);
WebTarget target = client.target("https://api.mydoforms.com/api/v2/");
System.out.println("Getting token");
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.username = "myusername";
tokenRequest.password = "mypassword";
Response tokenResponse = target.path("tokens/user").request(MediaType.APPLICATION_JSON)
.post(Entity.entity(tokenRequest, MediaType.APPLICATION_JSON));
// TODO: Check response code first for all requests
TokenResult tokenResult = tokenResponse.readEntity(TokenResult.class);
String auth = "Bearer " + tokenResult.token;
System.out.println("Getting projects");
List<Project> projects = target.path("projects").request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, auth).get(Response.class)
.readEntity(new GenericType<List<Project>>() {
});
for (Project project : projects) {
System.out.println("Project: " + project.name);
}
System.out.println("Getting forms for project " + projects.get(0).name);
List<Form> forms = target.path("projects/" + projects.get(0).key + "/forms").request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, auth).get(Response.class).readEntity(new GenericType<List<Form>>() {
});
for (Form form : forms) {
System.out.println("Form: " + form.name);
}
System.out.println("Getting definition for form " + forms.get(0).name);
Form form = target.path("forms/" + forms.get(0).key).request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, auth).get(Response.class).readEntity(Form.class);
for (Field field : form.fields) {
System.out.println("Field: " + field.name);
}
System.out.println("Getting submission " + form.id + "&&1");
Response submissionResponse = target.path("submissions/" + form.id + "&&1").request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, auth).get();
if (submissionResponse.getStatus() == Response.Status.OK.getStatusCode()) {
Submission submission = submissionResponse.readEntity(Submission.class);
System.out.println("Submission: " + submission.key);
} else {
ErrorResponse submissionError = submissionResponse.readEntity(ErrorResponse.class);
System.out.println("Submission error: " + submissionError.message);
}
}
}
Related content
C# REST Sample Code
C# REST Sample Code
More like this
doForms REST Web Service API
doForms REST Web Service API
More like this