发布于 2023-03-30 / 1479 阅读

Spring Boot Integration with Google Bard - Web API to access the Google AI chat box

Spring Boot Integration with Google Bard - Web API to access the Google AI chat box

I have created a Java library for Google Bard that can help us make a simple call to ask questions and get answers. Now I integrated the Java library into the Spring Boot application. So that we can make it publicly available to others.

Add Dependencies

Add the pkslow google-bard to the Spring Boot project:


Create a bean of GoogleBardClient

We need to create a bean of GoogleBardClient before you can use it to ask the questions:

public class GoogleBardConfig {

    public GoogleBardClient googleBardClient(@Value("${}") String token) {
        return new GoogleBardClient(token);


Inject the GoogleBardClient bean in the Controller, and get the question from request param.

public class BardController {
    private final GoogleBardClient client;

    public BardController(GoogleBardClient client) {
        this.client = client;

    public BardAnswer ask(@RequestParam("q") String question) {
        Answer answer = client.ask(question);
        if (answer.status() == AnswerStatus.OK) {
            return new BardAnswer(answer.chosenAnswer(), answer.draftAnswers());

        if (answer.status() == AnswerStatus.NO_ANSWER) {
            return new BardAnswer("No Answer", null);

        throw new RuntimeException("Can't access to Google Bard");


After we asked and got the answers, we will return the related DTO object.


Set the token from browser:


How to get the token?

Go to

  • F12 for console
  • Copy the values
    • Session: Go to Application → Cookies → __Secure-1PSID. Copy the value of that cookie.

Test with Postman

Question 1: How to be a good father?

Queston 2: what is



The code is on GitHub.