我有以下 Java 方法被注释为 Jersey @POST 来处理表单提交:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("created")
public Response createCustomer(@FormParam("username") String username,
@FormParam("name") String name, @FormParam("dob") Date dob)
throws ServletException, IOException {
URI uri = URI.create(uriInfo.getPath());
Response r;
Integer id;
try {
id = dbController.create(username, name, dob); // This may throw exception.
r = Response.created(uri).build();
} catch (DataAccessException ex) {
String errMsg = ex.getMessage();
// To do: how to send error message to AJAX so that client-side can see the error message?
}
return r;
}
表单提交后,如果有一些错误,我希望客户端用户看到后端 REST 服务发送的错误消息。如何编写代码以将消息从后端发送到前端?