I have created a REST service which retrieves data from the database as per the request made and returns it JSON format.
Now, I need to create a HTML page with a button that, when clicked, should get the appropriate data from the service. I have learnt that this can be done through ajax. But am not aware how to do it.
The service uses Spring Framework and Apache CXF and retrieves data from a Mysql database, if that matters.
Code i have added to create my client:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#driver").click(function(event){
$.getJSON('http://localhost:8080/CxfRestService/rest/employeeservices/getList');
});
});
});
</script>
</head>
<body>
<input type="button" id="driver" value="Get Employee Data" />
</body>
Do i need to place my HTML page into my Java project and add the related configuration in my web.xml
/beans.xml
or something?
Well, all your service does is respond to HTTP requests. So, you need to send one - either
document.location.href = <url>
), orXMLHTTPRequest
(aka AJAX) and parse the result, for which JQuery has built-in functionality.jquery.ajax
is a convenient wrapper over that.You don't need to connect you client-side stuff with the Java project in any way - REST is specifically designed to allow them to be independent.