如何快速学习:[1]Swift编程语言,Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Ojective-C*共同运行于MacOS和iOS平台,用于搭建基于苹果平台的应用程......
2023-03-17 350 编程语言
我在进行基于Spring boot方式进行配置Servlet时,发现系统并不能扫描并识别出我所配置的Servlet类,后来经过分析,调整了注解的详细方式,最后解决了问题,这里就给出我是如何解决Spring boot框架上使用注解方式配置Servlet时导致系统无法扫描出我定义的Servlet的原因和解决方式
创建一个Maven工程项目,并在该工程的Pom文件中增加如下的包依赖
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
modelVersion>4.0.0/modelVersion>
groupId>fantasy/groupId>
artifactId>balckbox/artifactId>
version>0.0.1-SNAPSHOT/version>
parent>
groupId>org.springframework.boot/groupId>
artifactId>spring-boot-starter-parent/artifactId>
version>1.5.9.RELEASE/version>
/parent>
dependencies>
dependency>
groupId>org.springframework.boot/groupId>
artifactId>spring-boot-starter-thymeleaf/artifactId>
/dependency>
dependency>
groupId>org.springframework.boot/groupId>
artifactId>spring-boot-devtools/artifactId>
optional>true/optional>
/dependency>
/dependencies>
properties>
java.version>1.8/java.version>
/properties>
build>
plugins>
plugin>
groupId>org.springframework.boot/groupId>
artifactId>spring-boot-maven-plugin/artifactId>
/plugin>
/plugins>
/build>
/project>
创建一个Servlet服务类(FantasyServlet.java),对外提供WEB Servlet服务,需要使用到注解WebServlet
package fantasy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/fantasy/hello")
public class FantasyServlet extends HttpServlet {
private static final long serialVersionUID = 1457739152440306935L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>Fantasy Servlet do get request.....");
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(">>>>Fantasy Servlet do post request.....");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.print("html>body>");
out.print("h1>I am Fantasy from FantasyServlet @" new Date() "/h1>");
out.print("/body>/html>");
out.flush();
out.close();
}
}
创建一个Spring boot应用运行的主类(Application.java)
在主类我们需要使用ServletComponentScan注解和SpringBootApplication注解
package fantasy.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
运行Application类,启动Spring boot应用
这时候可以我们观察日志,发现系统并没有扫描到我们配置的servlet:/fantasy/hello
-----------------------------------
2018-01-15 17:07:54.653 INFO 4100 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-01-15 17:07:54.653 INFO 4100 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1423 ms
2018-01-15 17:07:54.811 INFO 4100 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-15 17:07:54.814 INFO 4100 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-15 17:07:54.815 INFO 4100 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-01-15 17:07:54.815 INFO 4100 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-01-15 17:07:54.815 INFO 4100 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-15 17:07:55.081 INFO 4100 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12d912f: startup date [Mon Jan 15 17:07:53 CST 2018]; root of context hierarchy
2018-01-15 17:07:55.140 INFO 4100 --- [ restartedMai
我们通过浏览器访问Servlet服务也是失败的,提示找不到网页
系统扫描不到Servlet的问题分析和解决办法
1)问题分析
考虑到@ServletComponentScan注解是可以设置扫描的包路径的,而我们的主应用类Application和Servlet并不是在同一个包路径下,所以我增加了扫描路径的参数进行测试
package fantasy.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan("fantasy.servlet")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2)测试结果:日志显示了扫描到了我们的Servlet:/fantasy/hello
同时浏览器的页面也可以正常访问
以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!
标签: 编程语言
相关文章
如何快速学习:[1]Swift编程语言,Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Ojective-C*共同运行于MacOS和iOS平台,用于搭建基于苹果平台的应用程......
2023-03-17 350 编程语言
web图表开发工具FineReport:[11]连续分组,数据库表数据是按照时间先后录入的,查询的时候希望按照时间先后,某个字段连续相同的话就合并起来显示,这样的报表可以通过相邻连续分组来实现。......
2023-03-17 574 编程语言