I tried using GoogleHttpClient of Java

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.UrlEncodedContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson2.JacksonFactory;
import lombok.Data;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Main {

    static JsonFactory JSON_FACTORY = new JacksonFactory();
    static HttpRequestFactory httpRequestFactory = (new NetHttpTransport()).createRequestFactory();

    public static void main(String[] args) throws IOException {
        Map<String, Object> params = new HashMap<>();
        params.put("id", 1234);

        String url = "http://127.0.0.1:8080/user";
        User map = postHttpRequest(url, params, User.class);
        System.out.println(map);
    }

    private static <T> T postHttpRequest(String url, Map<String, Object> params, Class<T> clazz) throws IOException {
        HttpRequest req = httpRequestFactory.buildPostRequest(
                new GenericUrl(url),
                new UrlEncodedContent(params));
        req.setParser(new JsonObjectParser(JSON_FACTORY));

        String json = req.execute().parseAsString();
        return new ObjectMapper().readValue(json, clazz);
    }

    @Data
    public static class User {
        private String id;
    }
}

I wrote it. The Google guy is simple, but I felt that there were few Post samples.

req.execute().parseAs(Map.class)

I could do something like that, so I thought it would work if I passed the class, but I could do Map, but I couldn't do normal vo. ..

So I used jackson.

Recommended Posts

I tried using GoogleHttpClient of Java
I tried using Java REPL
I tried using JWT in Java
I tried using Elasticsearch API in Java
I tried using OpenCV with Java + Tomcat
I tried using Gson
I tried using TestNG
I tried using Galasa
I tried using the profiler of IntelliJ IDEA
I tried Drools (Java, InputStream)
I tried using Apache Wicket
I tried metaprogramming in Java
I tried using the Server Push function of Servlet 4.0
I tried to operate SQS using AWS Java SDK
I tried using Log4j2 on a Java EE server
I tried using Spring + Mybatis + DbUnit
I tried to summarize the basics of kotlin and java
Handling of time zones using Java
I tried to interact with Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried to summarize Java learning (1)
I tried using Dapr in Java to facilitate microservice development
I tried to make a client of RESAS-API in Java
[Android] I tried using Coordinator Layout.
I tried using Pari gp container
Summary of object-oriented programming using Java
I tried using WebAssembly Stadio (2018/4/17 version)
I tried to summarize Java 8 now
I tried the input / output type of Java Lambda ~ Map edition ~
I tried using the cache function of Application Container Cloud Service
I tried to summarize the methods of Java String and StringBuilder
[Java] I participated in ABC-188 of Atcorder.
I tried using Realm with Swift UI
I tried using Java's diagnostic tool Arthas
I tried using UICollectionViewListCell added from Xcode12.
I tried Cassandra's Object Mapper for Java
Java9 was included, so I tried jshell.
I tried using Scalar DL with Docker
I tried the new era in Java
I tried using OnlineConverter with SpringBoot + JODConverter
[day: 5] I summarized the basics of Java
It's new, but I tried using Groonga
Status monitoring of java application using Elasticsearch
Acquisition of input contents using Scanner (Java)
I tried Google's entrance exam (unofficial) [java]
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried to make a talk application in Java using AI "A3RT"
I tried FizzBuzz.
[Java] Overview of Java
I tried JHipster 5.1
I tried using Docker for the first time
I tried using Junit on Mac VScode Maven
[For beginners] I tried using DBUnit in Eclipse
I tried putting Java on my Mac easily
I tried barcode scanning using Rails + React + QuaggaJS
I tried to make Basic authentication with Java
[For beginners] I tried using JUnit 5 in Eclipse