当前位置: 首页 > 知识库问答 >
问题:

Spring Boot应用程序中的VaadinSession失败

上官高畅
2023-03-14

我正在尝试制作一个与Spring Boot/Spring Session/Spring Security集成的简单Vaadin应用程序,因为客户机的需求不是那么苛刻,所以没有什么特别的东西。我设法用Spring Session JDBC建立了一个基本环境,并添加了几个view

@SpringUI
@SpringViewDisplay
public class MyUI extends UI implements ViewDisplay {

    private Panel springViewDisplay;
    private static final long serialVersionUID = -5326618953003021331L;

    @Override
    protected void init(VaadinRequest request) {
        final VerticalLayout root = new VerticalLayout();
        root.setSizeFull();
        setContent(root);

        final CssLayout navigationBar = new CssLayout();
        navigationBar.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
        navigationBar.addComponent(createNavigationButton("Nómina",
            DefaultView.VIEW_NAME));
        navigationBar.addComponent(createNavigationButton("Empleados",
            EmpleadosAdminView.VIEW_NAME));
        root.addComponent(navigationBar);

        springViewDisplay = new Panel();
        springViewDisplay.setSizeFull();
        root.addComponent(springViewDisplay);
        root.setExpandRatio(springViewDisplay, 1.0f);
    }
}

defaultview加载良好:

@SpringView(name = DefaultView.VIEW_NAME)
public class DefaultView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "";
    private static final long serialVersionUID = 3600517630040771693L;

    @PostConstruct
    void init() {
        addComponent(new Label("Default view"));
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
    // This view is constructed in the init() method()
    }
}

但是,第二个视图在访问它时引发以下异常:

@SpringView(name = EmpleadosAdminView.VIEW_NAME)
public class EmpleadosAdminView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "empleados";
    private static final long serialVersionUID = 3344332615518673282L;

    private final EmpleadoRepository empleadoRepository;
    private final EmpleadosEditor empleadosEditor;

    private final Grid<Empleado> grid;

    private final TextField filter;

    private final Button addNewEmpleado;

    @Autowired
    public EmpleadosAdminView(EmpleadoRepository empleadoRepository, EmpleadosEditor empleadosEditor) {
        this.empleadoRepository = empleadoRepository;
        this.empleadosEditor = empleadosEditor;
        this.grid = new Grid<>(Empleado.class);
        this.filter = new TextField();
        this.addNewEmpleado = new Button("Agregar nuevo empleado", VaadinIcons.PLUS);
    }

    private void listEmpleados(String filterText) {
        if (StringUtils.isEmpty(filterText)) {
           grid.setItems(empleadoRepository.findAll());
        } else {
        grid.setItems(empleadoRepository.findByNombreIgnoreCase(filterText));
        }
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        HorizontalLayout actions = new HorizontalLayout(filter, addNewEmpleado);
        VerticalLayout mainLayout = new VerticalLayout(actions, grid, empleadosEditor);
        addComponent(mainLayout);

        grid.setHeight(300, Unit.PIXELS);
        grid.setColumns("id", "nombre", "cedula");

    filter.setPlaceholder("Filtrar por nombre");

    filter.setValueChangeMode(ValueChangeMode.LAZY);
    filter.addValueChangeListener(e -> listEmpleados(e.getValue()));

    grid.asSingleSelect().addValueChangeListener(e ->
            empleadosEditor.editEmpleado(e.getValue())
    );

    addNewEmpleado.addClickListener(e -> empleadosEditor.editEmpleado(new Empleado()));

    empleadosEditor.setChangeHandler(() -> {
        empleadosEditor.setVisible(false);
        listEmpleados(filter.getValue());
    });

    listEmpleados(null);
}
}

Hibernate:从Empleado empleado0_中选择empleado0_.id作为id1_0_,empleado0_.activo作为activo2_0_,empleado0_.cedula作为cedula3_0_,empleado0_.nombre作为nombre4_0_

2018-03-20 19:07:21.222警告13380--[nio-8080-exec-3]com.vaadin.event.ListenerMethod:应用程序序列化出错:类com.tomasa.nomina.ui.EmpleAdoseditor$$Lambda$658/1513980400必须实现序列化。

2018-03-20 19:07:21.230错误13380---[nio-8080-exec-3]com.vaadin.server.DefaulTerrorHandler:

我的代码基于以下教程:

http://vaadin.github.io/spring-tutorial/https://spring.io/guides/gs/crud-with-vaadin/

不确定我做错了什么,或者是否有一些配置需要修改。基本身份验证配置如下:

@Configuration
@EnableJdbcHttpSession
public class JdbcHttpSessionConfig {
}
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(SecurityConfig.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable() // Use Vaadin's CSRF protection
                .authorizeRequests().anyRequest().authenticated() // User must be authenticated to access any part of the application
                .and()
                .httpBasic();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/*"); // Static resources are ignored
    }
}

共有1个答案

龚联
2023-03-14

日志中的以下一行揭示了核心问题:

2018-03-20 19:07:21.222 WARN 13380 --- [nio-8080-exec-3] com.vaadin.event.ListenerMethod : Error in serialization of the application: Class com.tomasa.nomina.ui.EmpleadosEditor$$Lambda$658/1513980400 must implement serialization.

由于jdbcoperationssessionrepository在存储会话时使用标准Java序列化,因此必须确保放在会话中的属性实现serializable。在您的示例中,com.tomasa.nomina.ui.EmpleAdoseditor$$lambda$658/1513980400类看起来像是不符合该要求的冒犯者。

您可能应该仔细检查应用程序中最终放置在会话中的所有对象,并让它们实现serializable契约。

 类似资料:
  • 尝试在Heroku云中部署Spring Boot应用程序,但编译java应用程序时出现错误,但在我的本地计算机中运行良好。

  • 这是我使用SpringBoot的第一天,我试图理解体系结构,因此我开始构建一个hello world应用程序: 在我的pom.xml中,在maven-shade-plugin下,我将mainClass声明如下: 文件目标是src/main/java/com/demo/helloworld.java,该文件中的代码是: 我错过了什么?

  • 我需要在Spring-boot应用程序中使用默认的ObjectMapper作为单个实例。我是否可以简单地在应用程序内部@autowire ObjectMapper(在Spring-boot应用程序中默认创建的实例)而不创建@bean(因为我不想更改ObjectMapper的任何功能) https://docs.spring.io/spring-boot/docs/current/reference

  • 我试图在SpringMVC中运行SpringBoot应用程序,在SpringMVCPOM中添加SpringBoot应用程序依赖项,并扫描SpringBoot包,但我面临以下问题

  • 我有一个springboot应用程序,它根据LDAP验证身份验证。我通过邮递员测试了它,它工作得很好。我扩展了WebSecurityConfigurerAdapter以根据LDAP验证登录凭据。这是工作很好,没有任何问题。我有一个angular前端,我调用/login,并将用户名和密码发送到它。它没有工作,我得到一个CORs错误。我将下面的代码添加到application.java中,我再也看不到

  • 我想在应用程序运行时找到我的对象大小。我想用千分尺在Grafana中显示我的对象大小。 我的对象像人、学生、...... 我该怎么办? 对象大小像文件大小、对象体积