为Spring boot项目增加Servlet

为Spring boot项目增加Servlet有好多种方式

方式1

Just add a bean for the servlet. It’ll get mapped to /{beanName}/.

1
2
3
4
@Bean
public Servlet foo() {
return new FooServlet();
}

Note that if you actually want it mapped to /something/* rather than /something/ you will need to use ServletRegistrationBean

方式2

使用ServletRegistrationBean

1
2
3
4
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new FooServlet(),"/someOtherUrl/*");
}

如果想增加多个的话,就类似下面的方式

1
2
3
4
5
6
7
8
9
10
11
@Bean
public ServletRegistrationBean axisServletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*");
registration.addUrlMappings("*.jws");
return registration;
}

@Bean
public ServletRegistrationBean adminServletRegistrationBean() {
return new ServletRegistrationBean(new AdminServlet(), "/servlet/AdminServlet");
}

方式3

通过实现WebApplicationInitializer或者ServletContextInitializer或者ServletContainerInitializer接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
public class ConfigureWeb implements ServletContextInitializer, EmbeddedServletContainerCustomizer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerServlet(servletContext);
}

private void registerServlet(ServletContext servletContext) {
log.debug("register Servlet");
ServletRegistration.Dynamic serviceServlet = servletContext.addServlet("ServiceConnect", new ServiceServlet());

serviceServlet.addMapping("/api/ServiceConnect/*");
serviceServlet.setAsyncSupported(true);
serviceServlet.setLoadOnStartup(2);
}
}

方式4

如果使用内嵌的server的话,那么还可以使用@WebServlet WebServlet

Annotation used to declare a servlet.

This annotation is processed by the container at deployment time, and the corresponding servlet made available at the specified URL patterns.

1
2
@WebServlet(urlPatterns = "/example")
public class ExampleServlet extends HttpServlet

然后增加注解@ServletComponentScan:

1
2
3
4
@ServletComponentScan
@EntityScan(basePackageClasses = { ExampleApp.class, Jsr310JpaConverters.class })
@SpringBootApplication
public class ExampleApp

Please note that @ServletComponentScan will work only with embedded server:

Enables scanning for Servlet components (filters, servlets, and listeners). Scanning is only performed when using an embedded web server.

参考资料

Spring的AntPathMatcher是个好东西

经常需要在各种中做一些模式匹配,正则表达式虽然是个好东西,但是Ant风格的匹配情况也非常的多。
这种情况下使用正则表达式不一定方便,而Spring提供的AntPathMatcher确可以帮助我们简化很多。

位于Spring-core中的org.springframework.util.AntPathMatcher使用起来非常简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class AntPathMatcherTest {

private AntPathMatcher pathMatcher = new AntPathMatcher();

@Test
public void test() {
pathMatcher.setCachePatterns(true);
pathMatcher.setCaseSensitive(true);
pathMatcher.setTrimTokens(true);
pathMatcher.setPathSeparator("/");

Assert.assertTrue(pathMatcher.match("a", "a"));
Assert.assertTrue(pathMatcher.match("a*", "ab"));
Assert.assertTrue(pathMatcher.match("a*/**/a", "ab/asdsa/a"));
Assert.assertTrue(pathMatcher.match("a*/**/a", "ab/asdsa/asdasd/a"));


Assert.assertTrue(pathMatcher.match("*", "a"));
Assert.assertTrue(pathMatcher.match("*/*", "a/a"));
}
}

跨域资源共享以及Spring MVC的支持

本来想写文章详细整理一下「跨域资源共享,以及在Spring MVC中如何实现」,但是发现网站上已经有很多文章总结了,
相信我在怎么写也和绝大多数文章的差别不大,所以我选中了5篇比较不错的文章(地址在文章参考资料中)作为引用。
所以读者直接看引用的这几篇文章就好了。

##参考资料

Spring RestTemplate parse gzip response

假设http://10.89.xx.xx:8080/_/metrics接口返回的数据格式是gzip格式
他的Response Headers信息如下

1
2
3
4
5
6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Encoding: gzip
Content-Type: text/plain;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 28 Dec 2017 08:13:53 GMT

如果我们使用Spring RestTemplate想直接拿到String形式的返回,而不是byte[]格式,那么可以使用如下的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.apache.http.impl.client.HttpClientBuilder;
public static void main(String[] args) {
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://10.89.xx.xxx:8080/_/metrics", String.class);
HttpStatus statusCode = responseEntity.getStatusCode();
System.out.println(responseEntity.getBody());
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×