Thursday, April 18, 2024
HomeJavaThe way to return JSON, XML or Thymeleaf Views from Spring MVC...

The way to return JSON, XML or Thymeleaf Views from Spring MVC Controller? Instance Tutorial


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring boot ensures that Jackson is auto-configured to serialize and deserialize Java objects to JSON when utilizing this starter.

Then you need to annotate your
controller with @RestController to instruct spring to bind the response of your technique to the net
response.

@RestController
@RequestMapping("/studentjson")
public class JsonPayloadController {

    @GetMapping(path = "/college students", produces = MediaType.APPLICATION_JSON_VALUE)
    public Listing < Pupil > getStudents() {
        Listing < Pupil > college students = Listing.of(
            new Pupil("IT17092548", 12, "Grade C"),
            new Pupil("IT17092590", 8, "Grade D")
        );
        return college students;
    }
}

In order for you your controller to return a totally different HTTP standing, be certain your technique returns the Spring response wrapper
ResponseEntity.

@GetMapping(path = "/college students", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity < Listing < Pupil >> getOrders() {
    Listing < Pupil > college students = Listing.of(
        new Pupil("IT17092548", 12, "Grade C"),
        new Pupil("IT17092590", 8, "Grade D")
    );
    return ResponseEntity.standing(HttpStatus.SUCCESS).physique(college students);

}

The way to return a thymeleaf view from a controller

To begin with the thymeleaf, you need to add the next spring dependency
to your challenge.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Get the Thyemleaf dependency and the autoconfiguration mechanism of
spring boot ensures all required beans with right configuration are in
place. Spring could also be advised the view identify to render in quite a lot of strategies, the best
of which is to return the view identify as a string.

@Controller
@RequestMapping("/pupil")
public class StudentController {

    @GetMapping
    public String getStudentsPage(Mannequin mannequin) {
        mannequin.addAttribute("pupil", "Get all college students");
        return "all college students";
    }
}

The
Thymeleaf view resolver
searches for templates at
classpath:/templates/ with the
.html suffix.

<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>Welcome</title>
</head>

<physique>
    <div>
        <span th:textual content="${pupil}"></span>
    </div>
</physique>

</html>

You’ll be able to entry the HTML web page utilizing http://localhost:8080/welcome and returns and together with the message of scholars that has been set
within the studentController.

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>

In case you are utilizing the spring boot starter net, spring boot
ensures to auto-configure the whole lot to utilize the JacksonXmlModule. It’s worthwhile to annotate your POJO with @XmlRootElement.

@XmlRootElement
public class Pupil {

    non-public String id;

    // getters & setters

}
How to return to JSON, XML or Thymeleaf view from Spring MVC Controller?

Now you possibly can return XML out of your spring MVC controller strategies.

@GetMapping(path = "/college students", produces = MediaType.APPLICATION_XML_VALUE)
public Listing < Pupil > getStudents() {
    Listing < Pupil > college students = Listing.of(
        new Pupil("IT17092548", 12, "Grade C"),
        new Pupil("IT17092590", 8, "Grade D")
    );
    return college students;
}

That is all about
how you can return totally different sort of views from Spring MVC controller. So
on this tutorial, now we have mentioned the how-to return the JSON, XML, or
Thymeleaf view from the spring MVC controller with examples. So see you
within the subsequent tutorial with one other fascinating matter.

Different Spring MVC articles and Tutorials chances are you'll like

Thanks lots for studying this text up to now. In case you like this Java and Spring MVC tutorial then please share them with your mates and colleagues. You probably have any questions or suggestions then please drop a word. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments