我的任务是我需要在 Vaadin 应用程序中处理 post 请求 这是需要处理的请求的示例
我不明白如何在 Vaadin 中获取 POST 或 GET 请求 我还找到了有关 Creating a servlet 3.0 application 的文档 - 未找到 Vaadin v23
@Route("/")
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field")
@Log4j2
public class MainView extends VerticalLayout {
...
public MainView() {
// VaadinSession.getCurrent().addRequestHandler(
// new RequestHandler() {
// @Override
// public boolean handleRequest(VaadinSession session,
// VaadinRequest request,
// VaadinResponse response)
// throws IOException {
// if ("/notifications".equals(request.getPathInfo())) {
// // Generate a plain text document
// response.setContentType("text/plain");
// response.getWriter().append(
// "Here's some dynamically generated content.\n");
// response.getWriter().format(Locale.ENGLISH,
// "Time: %Tc\n", new Date());
//
// // Use shared session data
// response.getWriter().format("Session data: %s\n",
// session.getAttribute("mydata"));
//
// return true; // We wrote a response
// } else
// return false; // No response was written
// }
// });
}
@WebServlet(value = "/notifications", asyncSupported = true)
public static class Servlet extends VaadinServlet {
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
VaadinServlet servlet = getService().getServlet();
}
}
调用http://localhost:8080/notifications时 我明白了
Could not navigate to 'notifications'
在注释代码的帮助下,我设法进入请求处理程序,但我怀疑这是正确的方法
// VaadinSession.getCurrent().addRequestHandler(
// new RequestHandler() {
// @Override
// public boolean handleRequest(VaadinSession session,
// VaadinRequest request,
// VaadinResponse response)
// throws IOException {
// if ("/notifications".equals(request.getPathInfo())) {
// // Generate a plain text document
// response.setContentType("text/plain");
// response.getWriter().append(
// "Here's some dynamically generated content.\n");
// response.getWriter().format(Locale.ENGLISH,
// "Time: %Tc\n", new Date());
//
// // Use shared session data
// response.getWriter().format("Session data: %s\n",
// session.getAttribute("mydata"));
//
// return true; // We wrote a response
// } else
// return false; // No response was written
// }
// });
我还找到了有关创建 servlet 3.0 应用程序的文档 - 未找到 Vaadin v23
这应该仍然有效,因此您可以将应用程序映射到例如 http://localhost:8080/app/* 这样 Vaadin 就不会干扰 http://localhost:8080/notifications
我建议使用Spring Boot的逻辑(如果你的项目还没有依赖项,只需将它们包含在pom.xml中或通过gradle)
这是我使用的代码:
@PostMapping(path = "/user", consumes = "application/json", produces = "application/json")
String insert(@RequestBody User user) {
mUserService.save(new User(user.getId(), user.getName(), user.getAge()));
return "Data Inserted";
}
这是一个如何通过邮递员发布数据的示例: