How to invoke a REST service

2019-03-06 19:25发布

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?

1条回答
手持菜刀,她持情操
2楼-- · 2019-03-06 20:08

Well, all your service does is respond to HTTP requests. So, you need to send one - either

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.

查看更多
登录 后发表回答