使用Spark Java为库管理系统构建Restful Web服务

时间:2015-12-25 08:26:50

标签: java jquery html ajax

我是Spark Java的新手。我用一个用Java编写的get和post方法创建了一个Library Management System。我创建了一个HTML表单,它获取用户的信息并创建用户,这些用户存储在Java的ArrayList个对象中。当我调用get方法返回通过ajax创建的用户对象列表时,不会返回任何内容。成功也没有错误给出任何结果。

但是,当我只是把网址:http://localhost:4567/users 我得到以下输出:

[{"id":1,"firstName":"Bhavya","lastName":"Chauhan","age":"22","gender":"F","phone":"1234567890"},
{"id":2,"firstName":"Rashi","lastName":"Chauhan","age":"20","gender":"F","phone":"1233455677"}]

我附上了我的html和Spark java代码。提前谢谢!

<!DOCTYPE html>
<html>
<head>
<title>Text Input Control</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<meta charset="utf-8"/>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>    <script type="text/javascript"></script>

<script>

    $(function () {

        $("#userGet").click(function(){
            alert("Here");
            $.ajax({
                type: 'GET',
                url: 'http://localhost:4567/users',
                crossDomain: true,
                data: '',
                dataType: 'jsonp',
                async:false,
                success: function(responseData) {
                    //$("#divid").html(+responseData); 
                    alert("hi");
                },
                error: function(jqXHR, exception) {
                    if (jqXHR.status === 0) {
                        alert('Not connect.\n Verify Network.');
                    } else if (jqXHR.status == 404) {
                        alert('Requested page not found. [404]');
                    } else if (jqXHR.status == 500) {
                        alert('Internal Server Error [500].');
                    } else if (exception === 'parsererror') {
                        alert('Requested JSON parse failed.');
                    } else if (exception === 'timeout') {
                        alert('Time out error.');
                    } else if (exception === 'abort') {
                        alert('Ajax request aborted.');
                    } else {
                        alert('Uncaught Error.\n' + jqXHR.responseText);
                    }
                }

        });

    });

});

</script>

<body>

<h2>Library Management System </h2>
<button>Get Stuff</button>
<form>
    <h3>Manage Users </h3>
    <input type=button onClick="location='CreateUser.html'" value='Create User'>
    <br><br>
    <input type=submit id="userGet" value='Get All Users'>
    <br><br>
    <input type=button onClick="location='UpdateUser.html'" value='Update User'>
    <br><br>
</form>
<form action="http://localhost:4567/books" method="GET">
    <h3>Manage Books </h3>
    <input type=submit value='Get All Books'>
    <br><br>
    <input type=button onClick="location='FindBookByName.html'" value='Find Book By Name'>
    <br><br>
    <input type=button onClick="location='AddBook.html'" value='Add Book'>
    <br><br>
    <input type=button onClick="location='CheckOutBook.html'" value='Check Out Book'>
    <br><br>
</form>

<div id="divid"></div>

</body>
</html>

这是Java:

import static spark.Spark.*;
public class UserController extends JsonUtil
{
    public UserController(final UserService userService)
    {
        System.out.println("in user controller");
        get("/users", (req, res) -> userService.getAllUsers(), json());
        get("/books", (req, res) -> userService.getAllBooks(), json());
        post("/users", (req, res) -> userService.createUser(req.body()), json());
        post("/users/:id", (req, res) -> userService.updateUser(req.body()),json());
        post("/books", (req, res) -> userService.addBook(req.body()), json());
        post("/books/:name", (req, res) ->     userService.findBookByName(req.body()),json());
        post("/checkedOut", (req, res) -> userService.checkOutBook(req.body()), json());
    }
}

0 个答案:

没有答案