【问题标题】:Migrate Existing project to Spring Boot MVC multi Dispatcher from JSP, Servlet, etc将现有项目从 JSP、Servlet 等迁移到 Spring Boot MVC multi Dispatcher
【发布时间】:2018-05-07 17:04:22
【问题描述】:

我需要将旧项目转换为 Spring Boot 和 MVC。
在旧版本中使用的技术是 - JSP、Servlet、Hibernate、JPA、Jersey

我需要帮助才能轻松完成所有这些工作。

  1. 如何将web.xml 转换为SpringBoot Configauration,问题是-MVC 也有一个dispatcher

         <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
  1. 如何通过最少的代码更改来做到这一点?

    @Path("/aaccess")
    public class RestAAccess {
    @GET
    @Path("/get/{id}")
    @Produces("application/json")
    @Consumes("application/x-www-form-urlencoded")
    public Object get(@PathParam("id") int id) {
        //
    }
    
    @POST
    @Path("/doit")
    @Produces("application/json")
    public Object doit(@FormParam("id") int id,
                       @FormParam("role_id") int rid,
                       @FormParam("action") String action,
                       @FormParam("granted") boolean granted,
                       @FormParam("act") String act) {
        //
    }
    

    }

  2. 现有的道实现类(无接口)。如何改变这个

    public class CStaffData {
        public static List<CStaffResult> getByCompany(Session se, ResultPack rp, int id) {
           // --
                List<CStaffResult> table = se.createQuery("Some SQL").setParameter("id", id).setResultTransformer(Transformers.TO_LIST).list();
           //---    
        }
    }
    

【问题讨论】:

  • 考虑将其拆分为 3 个单独的问题:每个问题都是我们知识数据库的宝贵内容。他们每个人都应该有不同的倾向,以吸引将来遇到同样问题的人。 (你也可能会从每个人那里获得声望点数!)
  • 感谢 Dirk,提供宝贵的建议。但我认为答案可能很复杂并且相互关联。在您的情况下,有可能解决一个问题可能会与其他问题发生冲突。

标签: hibernate spring-mvc servlets spring-boot jersey


【解决方案1】:

我找到了解决问题的方法。

我从这里读到的第一个和第二个问题:
http://www.gauravbytes.com/2017/02/spring-boot-restful-webservices-with.html

将此部分添加到 pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

如果您已经使用tomcat embed,则不需要此部分。在一起是行不通的,只是为了提供信息。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

关于这个你可以在这里阅读(第 9 点):
http://www.baeldung.com/spring-boot-application-configuration

使用 Jersey 注释之后,您只需添加 @Component 注释。

您还必须添加注册球衣调度员:

@Configuration
@ApplicationPath("webapi")
public class JerseyConfiguration extends ResourceConfig {
  public JerseyConfiguration() {

  }

  @PostConstruct
  public void setUp() {
    register(RestAAccess.class);
  }
}

如果旧项目存在另一个 servlet,您也可以使用以下代码添加:x` (在这里阅读 - http://www.logicbig.com/tutorials/spring-framework/spring-boot/servlet-component-beans/

Servlet 类:

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        PrintWriter writer = resp.getWriter();
        writer.println("response from servlet ");
    }
}

配置函数(添加你的主类):

@Bean
ServletRegistrationBean myServletRegistration () {
    ServletRegistrationBean srb = new ServletRegistrationBean();
    srb.setServlet(new MyServlet());
    srb.setUrlMappings(Arrays.asList("/path2/*"));
    return srb;
}

关于 SessionFactory 解决方案未准备好(未测试)。以防万一,我会添加未来。

【讨论】:

    【解决方案2】:

    1:将server.contextPath=/webapi放入application.properties

    2:  
        @RestController
        @RequestMapping("/aaccess")
        public class RestAAccess {
    
        @RequestMapping(value="/get/{id}",method = RequestMethod.GET)
        public Object get(@PathVariable("id") int id) {
            //
        }
    
        @RequestMapping(value=""/doit"",method = RequestMethod.POST)
        public Object doit(@RequestBody SampleBody sb) {
            //
        }
    
    class SampleBody{
    
             int id,
    
             @JsonProperty(value = "role_id")int rid,
    
             @JsonProperty("action") String action;
    
             @JsonProperty("granted") boolean granted;
    
             @JsonProperty("act") String act;
    
          ...........getter setters ......
     }
    

    3:更好地使用 Respository 或 Dao 正确检查 spring 文档。

    【讨论】:

    • 问题 1 - 还有 MVC 调度程序,我将它用于 JSP。我的意思是,所有 JSP 页面都通过控制器并显示而无需扩展。在您的情况下,所有项目都将从 /webapi/* 问题 2 开始 - 如果可能的话,我需要使用现有注释而不进行更改。问题 3 - 我将解释详细信息,现有代码带有 Session 参数。在 Sring Boot Session 中不存在或其存在于后台。我自己也在研究寻找解决方案,如果找到我会通知解决方案。
    • 我找到了资源,似乎可以使用现有的注释 -dzone.com/articles/…
    • 1:是的,如果您更改上下文路径,它将为所有请求更改,您可以使用“webapi”启动所有 api 根资源路径,我的解决方案是根据您的代码 sn-p。 2,3 如果要保持所有代码相同,为什么要切换到 Spring 生态系统