Home > Enterprise >  Getting values from external API and displaying through another controller
Getting values from external API and displaying through another controller

Time:11-07

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?

CodePudding user response:

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

CodePudding user response:

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
    }
}
  • Related