0

I am using Spring Boot to define a REST-Api. I am looking for a way to declare a handler that will return a JSON-file.

Here is my idea:

In order to return a HTML-file, this file needs to be located inside resources --> templates. By defining a controller such as the following:

@Controller
public class LoginController {

    @GetMapping("/home")
    public String home() {
        return "home";
    }

and including the thymeleaf-dependency inside your Maven pom.xml, the HTML-file can be returned upon referencing domainname/home.

The same should be true for returning a JSON-file, but I don't know where to store this file. Is it suppossed to go inside templates as well?

Luk
  • 1,009
  • 2
  • 15
  • 33
  • I may have misunderstood the question, but there is (typically) a fairly basic difference between generating an HTML response using a Thymeleaf template (as a file) + model data from Java objects on the one hand, and generating a JSON response on the other hand. For the JSON response, there is (typically) no external template into which your data is placed. There is just the Java object hierarchy (e.g. a map or a list) which is directly converted to JSON by libraries such as Gson or Jackson. The JSON never (typically) exists anywhere as a file/template. – andrewJames Dec 02 '21 at 21:56
  • thx for your comment, andresJames! I know that typically a Java object would have been used for this purpose. And that's my final goal as well - retrieving the data from a database as a java object and return it in json format to my browser. But since I am completely new to all of this I just want to try out stuff and I found a video that explains how to load data from a json file into a table inside an html file. I just want to follow along, but don't know how to obtain this json file when using spring boot as my backend (the video did not explain that). – Luk Dec 03 '21 at 07:15
  • 1
    Understood. Take a look at questions such as [Read file from resources folder in Spring Boot](https://stackoverflow.com/questions/44399422/read-file-from-resources-folder-in-spring-boot) - and various other similar questions. Try some of the suggested approaches. If you still have a problem you can ask a new (and more specific) question. – andrewJames Dec 03 '21 at 13:36
  • allright, thx a lot! – Luk Dec 03 '21 at 15:54

0 Answers0