Jersey2 中的@PUT 和@Post
@PUT and @Post in Jersey2
我的配置出现 415 错误 Spring Boot,JQuery,Html templates jn WildFly 10。
@PUT 和@POST:
@POST
//@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
public Response createCustomers(@FormParam("firstname") String firstname,
@FormParam("lastname") String lastname,
@FormParam("email") String email,
@FormParam("dateborn") String dateborn,
@FormParam("pass") String pass,
@Context UriInfo uriInf
){
CustomersEntity customer = new CustomersEntity();
customer.setFirstname(firstname);
customer.setLastname(lastname);
customer.setEmail(email);
customer.setDateborn(dateborn);
customer.setPass(pass);
customerService.save(customer);
long id = customer.getId();
URI createdUri = uriInf.getAbsolutePathBuilder().path(Long.toString(id)).build();
return Response.created(createdUri).build();
}
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateCustomers(@PathParam("id") Long id,
@FormParam("customerFn") String firstname,
@FormParam("customerLn") String lastname,
@FormParam("customerEmail") String email,
@FormParam("customerDb") String dateborn,
@FormParam("customerPass") String pass
) {
CustomersEntity inDb = customerService.findOne(id);
if (inDb == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
else {
inDb.setFirstname(firstname);
inDb.setLastname(lastname);
inDb.setEmail(email);
inDb.setDateborn(dateborn);
inDb.setPass(pass);
customerService.update(inDb);
}
return Response.noContent().build();
}
HTML形式:
<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>
部分JQuery:
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');
}
});
}
我得到:
响应头:
状态代码:415 不支持的媒体类型
连接:保持活动状态
日期:2017 年 6 月 12 日星期一 15:33:06 GMT
服务器:WildFly/10
传输编码:分块
X-Powered-By:Undertow/1
异常:
Caused by: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded.
其他方法(所有@GET-s 和@DELETE)工作正常。
如错误消息中所述,您不能将 @FormParam
与 application/x-www-form-urlencoded
以外的内容类型一起使用。
经过对这个问题的反思和研究,我找到了答案。 Jax-rs Jarsey2 配置从请求正文中提取数据和数据格式不需要使用额外的注释从 HTML-form:
转换
@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();
}
我的配置出现 415 错误 Spring Boot,JQuery,Html templates jn WildFly 10。 @PUT 和@POST:
@POST
//@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
public Response createCustomers(@FormParam("firstname") String firstname,
@FormParam("lastname") String lastname,
@FormParam("email") String email,
@FormParam("dateborn") String dateborn,
@FormParam("pass") String pass,
@Context UriInfo uriInf
){
CustomersEntity customer = new CustomersEntity();
customer.setFirstname(firstname);
customer.setLastname(lastname);
customer.setEmail(email);
customer.setDateborn(dateborn);
customer.setPass(pass);
customerService.save(customer);
long id = customer.getId();
URI createdUri = uriInf.getAbsolutePathBuilder().path(Long.toString(id)).build();
return Response.created(createdUri).build();
}
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateCustomers(@PathParam("id") Long id,
@FormParam("customerFn") String firstname,
@FormParam("customerLn") String lastname,
@FormParam("customerEmail") String email,
@FormParam("customerDb") String dateborn,
@FormParam("customerPass") String pass
) {
CustomersEntity inDb = customerService.findOne(id);
if (inDb == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
else {
inDb.setFirstname(firstname);
inDb.setLastname(lastname);
inDb.setEmail(email);
inDb.setDateborn(dateborn);
inDb.setPass(pass);
customerService.update(inDb);
}
return Response.noContent().build();
}
HTML形式:
<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>
部分JQuery:
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');
}
});
}
我得到:
响应头: 状态代码:415 不支持的媒体类型 连接:保持活动状态 日期:2017 年 6 月 12 日星期一 15:33:06 GMT 服务器:WildFly/10 传输编码:分块 X-Powered-By:Undertow/1
异常:
Caused by: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded.
其他方法(所有@GET-s 和@DELETE)工作正常。
如错误消息中所述,您不能将 @FormParam
与 application/x-www-form-urlencoded
以外的内容类型一起使用。
经过对这个问题的反思和研究,我找到了答案。 Jax-rs Jarsey2 配置从请求正文中提取数据和数据格式不需要使用额外的注释从 HTML-form:
转换 @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();
}