Spring Boot JSP call Spring Boot Web Service by using Json

IntelliJ Spring Boot Application using JSP and Web Service

Spring Boot JSP call Spring Boot Web Service by using Json 

 

REST Web Service, receive json TalkData , execute a Linux script(command is in TalkData) 

 

@RestController 

public class CallingController 

{ 

    @RequestMapping("/") 

    String name(){return "hello it is caller for Linux script";} 

 

    @PostMapping(path = "/call2") 

    public String runScript2(@RequestBody TalkData talkData) 

    { 

        try { 

            Process proc = new ProcessBuilder(talkData.getCommandName(), "").start(); 

            try { 

                InputStream is = proc.getInputStream(); 

                int i = -1; 

                StringBuilder buf = new StringBuilder(); 

                while ((i = is.read()) != -1) { 

                    buf.append((char)i); 

                } 

                proc.waitFor(5, TimeUnit.SECONDS); 

                return buf.toString(); 

            } catch (InterruptedException e) { 

                e.printStackTrace(); 

                return "InterruptedException " + e.getMessage() + "!"; 

            } 

        } catch (IOException e) { 

            e.printStackTrace(); 

            return "IOException " + e.getMessage() + "!"; 

        } 

    } 

} 

 

Spring Boot Web call the Web Service 

Key point is the two lines 

 

HttpEntity<TalkDatarequestEntity = new HttpEntity<TalkData>(talkData);//modify 

ResponseEntity<String> response = restTemplate.postForEntity(resourceURL,requestEntity,String.class);//modify 

 

Spring Boot Web Code as below: 

 

 

@Controller 

public class UserController { 

    @Autowired 

    private My my; 

 

    @PostMapping("/user") 

    @RequestMapping(params = "memory", method = RequestMethod.POST) 

    public String selectLinuxClientGetMemory(String hostnamefinal RedirectAttributes attributes) { 

 

        List<My.ServerserverList = my.getServers(); 

        int selectIndex = -1; 

 

        for (int index=0; index < serverList.size();index++) 

        { 

            if (serverList.get(index).getName().equals(hostname)) 

            { 

                selectIndex = index; 

                break; 

            } 

        } 

 

        if (selectIndex == -1) 

        { 

            return null; 

        } 

 

        String commandName = "/home/oracle/test3"; 

 

        String ip = serverList.get(selectIndex).getIp() + ":8084"; 

 

        TalkData talkData = new TalkData(); 

        talkData.setCommandName(commandName); 

        HttpEntity<TalkDatarequestEntity = new HttpEntity<TalkData>(talkData);//modify 

 

        RestTemplate restTemplate = new RestTemplate(); 

        HttpHeaders headers = new HttpHeaders(); 

        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); 

        String resourceURL = "http://" + ip + "/call2"; 

 

        HttpEntity<String> entity = new HttpEntity<String>(headers); 

         

        ResponseEntity<String> response = restTemplate.postForEntity(resourceURL,requestEntity,String.class);//modify 

 

        String result = ""; 

 

        if (response.getStatusCode() == HttpStatus.OK) 

        { 

            System.out.println(response.getBody()); 

 

            result = response.getBody(); 

        } 

        else 

        { 

            System.out.println("Error"); 

         

        } 

        attributes.addFlashAttribute("hostname",hostname); 
        attributes.addFlashAttribute("result""MemFree  " + result + " kb"); 

 

        return "redirect:/user"; 

    } 

 

 

TalkData.java as below, include commandName: 

 

@Component 

public class TalkData 

{ 

    private String commandName; 

 

    public String getCommandName() { 

        return commandName; 

    } 

 

    public void setCommandName(String commandName) { 

        this.commandName = commandName; 

    } 

 

    public TalkData() { 

    } 

} 

 

 

My.java as below: 

Class My include Server List 

 

@Component 

@ConfigurationProperties(prefix = "my") 

public class My { 

 

    private List<Server> servers; 

 

    public List<Server> getServers() { 

        return servers; 

    } 

 

    public void setServers(List<Server> servers) { 

        this.servers = servers; 

    } 

 

    public My() { 

    } 

//getters and setters 

 

    public static class Server 

    { 

        private String name; 

        private String ip; 

 

        public String getName() { 

            return name; 

        } 

 

        public void setName(String name) { 

            this.name = name; 

        } 

 

        public String getIp() { 

            return ip; 

        } 

 

        public void setIp(String ip) { 

            this.ip = ip; 

        } 

 

        @Override 

        public String toString() { 

            return "Server{" + 

                    "name='" + name + '\'' + 

                    ", ip='" + ip + '\'' + 

                    '}'; 

        } 

    } 

} 

 

 

application.yml as below: 

define the server array 

 

my: 
  servers: 
    - name: Linux1 
      ip: 192.168.12.3 
 
    - name: Linux2 
      ip: 192.168.12.4 
 
    - name: Linux3 
      ip: 192.168.12.5 

 

 

user.jsp as below: 

 

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 

<html> 

<head> 

    <title>Title</title> 

</head> 

 

<body> 

 

<form:form method = "POST" action = "/user"> 

 

    <form:label path = "hostname">hostname</form:label> 

 

    <form:select path = "hostname"> 

        <form:option value = "NONE" label = "Select"/> 

        <form:options items = "${hostnameList}" /> 

    </form:select> 

    <input type="submit" class="button" name="submit" value="Submit"/> 

    <input type="submit" class="button" name="memory" value="GetMemory"/> 

</form:form> 

 

<h1>${hostname}</h1> 

<br /> 

<h2>${result}</h2> 

 

</body> 

</html> 

 

 

UserController.java as below: 

Add Server List by application.yml server list 

 

    @ModelAttribute("hostnameList") 

    public Map<String, String> getHostnameList() 

    { 

        Map<String, String> hostnameList = new HashMap<String, String>(); 

        List<My.Server> serverList = my.getServers(); 

        for (My.Server s :serverList) 

        { 

            hostnameList.put(s.getName(), s.getName()); 

        } 

        return hostnameList; 

    } 

 

Reference: 

Using the Spring RestTemplate Interceptor