1

I have a spring boot project with 2 controller files as below:

File1.java

@GetMapping(value = "/test")
public List<Object> getdata() {
    String e_url = ""; //external API with above JSON data
    RestTemplate rt = new RestTemplate();
    Object[] test = rt.getForObject(e_url,Object[].class);
    return Arrays.asList(test);
}

File2.java

public class controller2 extends HttpServlet {
    Public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException {}
    Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
    Public static void pr(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //From here I want to display the JSON data to web interface which passes from controller 1
    }
}

When I run it throws this error

HTTP Status 500 – Internal Server Error and shows an error on the Object[] test = rt.getForObject(e_url,Object[].class); line .

I want to show JSON Data on the web interface through another controller and connect 1st and 2nd controller through the POST method

The JSON format is:

{
  "seconds": 0.00163,
  "records": [
    {
      "id": "cc:1001",
      "column": "col:10",
      "idn": "tester.",
      "topic": "W",
      "rnk": 2,
      "tid": "txn:218",
      "stp": "M"
    }
  ]
}

Could you please give some suggestions on how I could accomplish this?

João Dias
  • 16,277
  • 6
  • 33
  • 45
s21s
  • 19
  • 1
  • 8
  • Why do you want to provide the data from another controller? Can't you simply return the data from the controller in `File1.java`? Otherwise, your application will need to have state, storing the retrieved information in memory or in a database. – João Dias Nov 06 '21 at 20:19
  • @JoãoDias Can't I directly relay my incoming JSON data to another controller without storing it in DB? Because my problem ask me to relay information from file1.java to file2.java and then displaying JSON data from file2.java to web interface? – s21s Nov 06 '21 at 20:26
  • You can, but without a database you will need to store the information in memory. I will add an example as soon as possible. – João Dias Nov 06 '21 at 21:13

2 Answers2

1

You should add an URL pattern to your second controller, using the @WebServlet annotation on the class-level. See more in this answer.

Petar Bivolarski
  • 1,599
  • 10
  • 19
1

If you don't want to use a database, then your other option is to store the date in memory as follows:

@RestController
public class Controller1 {

    private Service service;

    public Controller1(Service service) {
        this.service = service;
    }

    @GetMapping(value = "/test")
    public List<Object> getdata() {
        return service.getData();
    }
}

Your Controller would rely on a Service to get the data from another API as follows:

@Service
public class Service {

    private List<Object> data = new ArrayList<>();

    public List<Object> getdata() {
        String e_url = ""; //external API with above JSON data
        RestTemplate rt = new RestTemplate();
        Object[] test = rt.getForObject(e_url,Object[].class);
        data = Arrays.asList(test);
        return data;
    }

    public List<Object> getRetrievedData() {
        return data;
    }
}

Now, in your Controller2 you could get the retrieved data from the Service:

public class controller2 extends HttpServlet {

    private Service service; // Not really sure how to inject this here since for whatever 
        // reason you are using an `HttpServlet` instead of a regular Spring Boot Controller
    
    Public void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException {}
    Public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        service.getRetrievedData();
    }
    Public static void pr(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        //From here I want to display the JSON data to web interface which passes from controller 1
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45