怎么改spring boot http port

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么改spring boot http port相关的知识,希望对你有一定的参考价值。

1. servlet配置
当应用只有默认的servlet(即DispatcherServlet)时,映射的url为/,存在其他的servlet时,映射的路径为servlet的注册的beanname(可通过@Component注解修改),创建方式如下:

[java] view plain copy
@Component("myServlet")
public class MyServlet implements Servlet
/**
*
* @see javax.servlet.Servlet#destroy()
*/
@Override
public void destroy()
System.out.println("destroy...");

/**
* @return
* @see javax.servlet.Servlet#getServletConfig()
*/
@Override
public ServletConfig getServletConfig()
return null;

/**
* @return
* @see javax.servlet.Servlet#getServletInfo()
*/
@Override
public String getServletInfo()
return null;

/**
* @param arg0
* @throws ServletException
* @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
*/
@Override
public void init(ServletConfig arg0) throws ServletException
System.out.println("servlet init...");

/**
* @param arg0
* @param arg1
* @throws ServletException
* @throws IOException
* @see javax.servlet.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
*/
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException,
IOException
System.out.println("service...");


2. filter配置
filter配置的映射是/*,创建方式如下:

[java] view plain copy
@Component("myFilter")
public class MyFilter implements Filter
/**
*
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy()
System.out.println("destroy...");

/**
* @param arg0
* @param arg1
* @param arg2
* @throws IOException
* @throws ServletException
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException,
ServletException
System.out.println("doFilter...");
arg2.doFilter(arg0, arg1);

/**
* @param arg0
* @throws ServletException
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig arg0) throws ServletException
System.out.println("filter init...");


<span style="font-family: Arial, Helvetica, FreeSans, sans-serif; font-size: 13.3333330154419px; line-height: 17.3333339691162px; background-color: transparent;"> </span>

3. listener配置

[java] view plain copy
@Component("myListener")
public class MyListener implements ServletContextListener
/**
* @param arg0
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent arg0)
System.out.println("contextDestroyed...");

/**
* @param arg0
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent arg0)
System.out.println("listener contextInitialized...");



如果觉得控制力度不够灵活(例如你想修改filter的映射),spring boot还提供了 ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean这3个东西来进行配置

修改filter的映射

[java] view plain copy
@Configuration
public class WebConfig
@Bean
public FilterRegistrationBean filterRegistrationBean(MyFilter myFilter)
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(myFilter);
filterRegistrationBean.setEnabled(true);
filterRegistrationBean.addUrlPatterns("/bb");
return filterRegistrationBean;



4. 配置servlet 容器

可以通过两种方式配置servlet容器,一种是通过properties文件,例如:

[java] view plain copy
server.port=8081
server.address=127.0.0.1
server.sessionTimeout=30
server.contextPath=/springboot
完整的配置信息可以看这 http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

另一种方式是Java代码的形式:

[java] view plain copy
@Component
public class MyCustomizationBean implements EmbeddedServletContainerCustomizer
/**
* @param container
* @see org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer#customize(org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer)
*/
@Override
public void customize(ConfigurableEmbeddedServletContainer container)
container.setContextPath("/sprintboot");
container.setPort(8081);
container.setSessionTimeout(30);



5. error 处理

spring boot 提供了/error映射来进行错误处理,它会返回一个json来对错误信息进行描述(包含了http状态和异常信息),例如404的错误

当然可以定制错误页面,通过实现ErrorController接口,并将它装配起来即可,如下:

[java] view plain copy
@Controller
public class ErrorHandleController implements ErrorController
/**
* @return
* @see org.springframework.boot.autoconfigure.web.ErrorController#getErrorPath()
*/
@Override
public String getErrorPath()
return "/screen/error";


@RequestMapping
public String errorHandle()
return getErrorPath();


还可以这样:

[java] view plain copy
@Component
private class MyCustomizer implements EmbeddedServletContainerCustomizer

@Override
public void customize(ConfigurableEmbeddedServletContainer container)
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/screen/error"));



6.模板引擎

spring boot 会自动配置 FreeMarker,Thymeleaf,Velocity,只需要在pom中加入相应的依赖即可,例如应用Velocity

[html] view plain copy
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>
默认配置下spring boot会从src/main/resources/templates目录中去找模板
参考技术A 配置文件指定端口即可
server.port=8889

spring boot 添加监控

在application.properties中,添加

        spring.application.name=Spring Boot Admin Web
        spring.boot.admin.url=http://localhost:${server.port}
        spring.jackson.serialization.indent_output=true
        endpoints.health.sensitive=false
        management.security.enabled=false
        ribbon.ReadTimeout=60000
        ribbon.ConnectTimeout=60000

在 启动类中添加

            @EnableAdminServer

在 pom.xml中添加

                <dependency>
                                    <groupId>de.codecentric</groupId>
                                    <artifactId>spring-boot-admin-server</artifactId>
                                    <version>1.5.7</version>
            </dependency>

            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-server-ui</artifactId>
                <version>1.5.7</version>
            </dependency>

            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>1.5.7</version>
            </dependency>

以上是关于怎么改spring boot http port的主要内容,如果未能解决你的问题,请参考以下文章

spring boot 添加监控

Spring Boot同时开启HTTP和HTTPS服务

Spring Boot 将 HTTP 重定向到 HTTPS

spring boot之支持http和https并行(http不跳转)

使用属性 server.port=0 运行 spock 测试时如何找到 Spring Boot 容器的端口

spring boot 修改连接地址和端口