D
D
Denis Kuznetsov2021-02-15 18:29:53
Java
Denis Kuznetsov, 2021-02-15 18:29:53

How to work with rest api using json and user input at the same time?

Hello, I have a rest controller that works with data, receiving and issuing them in json format:

@GetMapping(value = "/employee/{empNo}",
                produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Employee getEmployee(@PathVariable("empNo") String empNo){
        return employeeDAO.getEmployee(empNo);
    }

    @PostMapping(value = "/employee",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Employee addEmployee(@RequestBody Employee emp) {
        logger.info("(Service Side) Creating employee: " + emp.getEmpNo());
        return employeeDAO.addEmployee(emp);
    }

and other methods.
But what if now I want, say, to add html pages with user input and also display data to users, say, in the form of a table, and not in json format.
<form th:action="@{/register}"
      th:object="${personForm}" method="POST">

To do this, I probably need to solve the following questions:
1) how to organize the html page so that when submit, json is formed from the fields entered by the user, so that if I wish, I can immediately transfer json there via postman and create a new object
2) how to return the object so that when a user request, the user received the output as a table, and when requesting a third-party service, this service received json.
Perhaps I do not quite understand the principle of operation, so I will be very grateful for the accompanying explanations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-02-15
@DennisKingsman

It's called: "Write a frontend to an existing backend."
We make an html form, take a ToadScript, write a form submission event handler, in it, instead of the default behavior, we send an html request to your server, where the back is spinning, we receive data in the response, generate a table from them, add it to the page. And voila: you are a full stack developer! :).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question