Jersey2中的@PUT和@Post

时间:2017-06-12 15:39:11

标签: spring-boot jax-rs jersey-2.0

我在配置Spring Boot,JQuery,Html模板jn WildFly 10中得到415错误。 @PUT和@POST:

<form id="customerForm" method="POST" action="/customers">

    <div class="mainArea">

        <label>Id:</label>
        <input id="custId" name="id" type="text" disabled="disabled" />

        <label>First Name:</label>
        <input type="text" id="custFn" name="customerFn" required="required" />

        <label>Last Name:</label>
        <input type="text" id="custLn" name="customerLn" />

        <label>Email:</label>
        <input type="text" id="custEmail" name="customerEmail" />

        <label>Date Born:</label>
        <input type="text" id="custDb" name="customerDb" />

        <label>Pass:</label>
        <input type="text" id="custPass" name="customerPass" />

        <button id="btnSaveCustomer">Save</button>
        <button id="btnDeleteCustomer">Delete</button>
    </div>
</form>

HTML表单:

function addCustomer() {
    console.log('addCustomer');
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: customerlistURL,// + '/create',
        dataType: "json",
        data: formToJSON(),
        success: function(data, textStatus, jqXHR){
            alert('Customer created successfully');
            $('#btnDeleteCustomer').show();
            $('#custId').val(data.id);
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('addCustomer error: ' + textStatus);
        }
    });
}

function updateCustomer() {
    console.log('updateCustomer');
    $.ajax({
        type: 'PUT',
        contentType: 'application/json',
        url: customerlistURL + '/' + $('#custId').val(),
        dataType: "json",
        data: formToJSON(),
        success: function(data, textStatus, jqXHR){
            alert('Customer updated successfully');
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('updateCustomer error: ' + textStatus);
        }
    });
}

function deleteCustomer() {
    console.log('deleteCustomer ' + $('#custId').val());
    $.ajax({
        type: 'DELETE',
        url: customerlistURL + '/' + $('#custId').val(),
        success: function(data, textStatus, jqXHR){
            alert('Customer deleted successfully');
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('deleteCustomer error');
        }
    });
}

部分JQuery:

Caused by: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded.

我得到了:

响应标题:     状态代码:415不支持的媒体类型     连接:保持活力     日期:2017年6月12日星期一15:33:06 GMT     服务器:WildFly / 10     转移编码:分块     X-Powered-By:Undertow / 1

例外:

>>> x = 7
>>> if x > 5:
...print("five")

其他方法(所有@ GET-s和@DELETE)都可以正常工作。

2 个答案:

答案 0 :(得分:0)

如错误消息中所述,您不能将@FormParamapplication/x-www-form-urlencoded以外的内容类型一起使用。

答案 1 :(得分:0)

经过对这个问题的一些反思和研究,我找到了答案。 Jax-rs Jarsey2配置从请求主体中提取数据和数据格式,无需使用其他注释从HTML表单进行转换:

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateCustomers(@PathParam("id") Long id,
                                    CustomersEntity customer){
        CustomersEntity existCustomer = customerService.findOne(id);
        if (existCustomer == null){
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        else {
            existCustomer.setFirstname(customer.getFirstname());
            existCustomer.setLastname(customer.getLastname());
            existCustomer.setEmail(customer.getEmail());
            existCustomer.setDateborn(customer.getDateborn());
            existCustomer.setPass(customer.getPass());
            customerService.update(customer);
        }
        return Response.noContent().build();
    }