Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
What's the best practice for manipulating API response from Java using JavaScript?

I'm using Java Spring Boot for the backend, and Thymeleaf for the frontend. I'm using an API to retrieve and display thousands of images. What I am getting as a response is one JSON object that has thousands of subobjects that each has a link to the image.

I would like to send this big JSON object to the frontend. I'm trying to make an image slider. When I click the '>' button, the next image would appear. '<' button would show the previous image.

This is how I am sending an API response to the frontend. 'model' is an instance of ModelMap.

model.put("apiResponse", apiResponse);

apiResponse object is now accessible from the frontend HTML file. However, I'm not sure how I could manipulate this with JavaScript. Is it that all Java objects can't be handled using JavaScript directly?

In that case, is the best practice to use some kind of library to convert Java objects to JSON? I was wondering if I should do it because it sounds a bit weird:

  1. Originally, the API sends a JSON object.
  2. Java converts it to a Java model using List.
  3. Then Java server sends it to the frontend.
  4. In order to manipulate that using JavaScript, use the JS library to convert it back to JSON.

Isn't it weird that original data is JSON but I have to go through so many steps to make it back to the JSON? Is this really how things work? If not, what's a better way of doing things?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I solved this issue by using Thymeleaf Script Inlining, as suggested by its official documentation.

<script th:inline="javascript">
/*<![CDATA[*/
    var apiResponse = /*[[${apiResponse}]]*/ 'apiResponse';
/*]]>*/
</script>

After that, I was able to access the apiResponse variable in the external js file as well.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda