How do I make a method in the Spring controller that does the same as this method of a Servlet?

Asked

Viewed 599 times

0

This code returns a json for an html page that uses an ajax resource to load a Chart from the Google API I’d like to do the same but using a Spring controller!

 public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        List<Student> listOfStudent = getStudentData();

        Gson gson = new Gson();

        String jsonString = gson.toJson(listOfStudent);

        response.setContentType("application/json");

        response.getWriter().write(jsonString);

    }

This excerpt of code was taken from this website.

I would love to do this using spring with Thymeleaf!

Here is the treixo of my page that calls the controller:

   <li><a href="#" th:href="@{/student/jsonData}">Chart Student</a></li>

I created this class:

public class Student {

    private String name;
    private int computerMark;
    private int  mathematicsMark;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getComputerMark() {
        return computerMark;
    }

    public void setComputerMark(int computerMark) {
        this.computerMark = computerMark;
    }

    public int getMathematicsMark() {
        return mathematicsMark;
    }

    public void setMathematicsMark(int mathematicsMark) {
        this.mathematicsMark = mathematicsMark;
    }

Here’s the controller class I tried to do but it doesn’t work:

 import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("student")
public class StudentJsonData {

    @RequestMapping(value = "/jsonData", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody
    ModelAndView showChart() {

        ModelAndView view = new ModelAndView("student/visualization-chart-demo");

        List<Student> listOfStudent = getStudentData();

        Gson gson = new Gson();

        String jsonString = gson.toJson(listOfStudent);

        view.addObject("jsonString", jsonString);

        return view;
    }

    private List<Student> getStudentData() {

        List<Student> listOfStudent = new ArrayList<Student>();
        Student s1 = new Student();
        s1.setName("Sandeep");
        s1.setComputerMark(75);
        s1.setMathematicsMark(26);
        listOfStudent.add(s1);

        Student s2 = new Student();
        s2.setName("Bapi");
        s2.setComputerMark(60);
        s2.setMathematicsMark(63);
        listOfStudent.add(s2);

        Student s3 = new Student();
        s3.setName("Raja");
        s3.setComputerMark(40);
        s3.setMathematicsMark(45);
        listOfStudent.add(s3);

        Student s4 = new Student();
        s4.setName("Sonu");
        s4.setMathematicsMark(29);
        s4.setComputerMark(78);
        listOfStudent.add(s4);

        return listOfStudent;
    }
}

In the article js file I modified only this line of all the code:

$.ajax({

    url: "/springsecurity/student/jsonData",
...
...

This is my page where I wanted to render Chart:

<html
    xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Google Visualization Chart Demo</title>
    </head>

    <body>
        <div id="student-bar-chart"></div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script  th:src="@{/resources/js/visualization-chart-script.js}" ></script> 

    </body>
</html>

I’m definitely messing with my controller!! I put the attribute produces = "application/json" because I want to return a json. I’ve researched it all over the place and couldn’t find how to do it! I’ve tried everything, but I can’t get Chart on the page!! Surely it must be the problem my controller method, because in the application through the menu I call you the method that returns a view that is the page where I want to display the data, and this page has the js file which has jquery which has ajax which uses the url I modified from the file to call /springsecurity/student/jsonData.

I guess that’s the point where I get lost.

  • @Andersoncarloswoss I think now I’ve described everything I’ve tried to do!

1 answer

0


Tend to feel the example below.

@RestController
@RequestMapping(value = "/student")
public class StudentJsonData
{
    @RequestMapping(value = "/jsonData", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public String jsonData()
    {
         List<Student> listOfStudent = getStudentData();

        Gson gson = new Gson();

        return gson.toJson(listOfStudent);
    }

    private List<Student> getStudentData() {

        List<Student> listOfStudent = new ArrayList<Student>();
        Student s1 = new Student();
        s1.setName("Sandeep");
        s1.setComputerMark(75);
        s1.setMathematicsMark(26);
        listOfStudent.add(s1);

        Student s2 = new Student();
        s2.setName("Bapi");
        s2.setComputerMark(60);
        s2.setMathematicsMark(63);
        listOfStudent.add(s2);

        Student s3 = new Student();
        s3.setName("Raja");
        s3.setComputerMark(40);
        s3.setMathematicsMark(45);
        listOfStudent.add(s3);

        Student s4 = new Student();
        s4.setName("Sonu");
        s4.setMathematicsMark(29);
        s4.setComputerMark(78);
        listOfStudent.add(s4);

        return listOfStudent;
    }
}
  • Thanks man, for your reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.