SpringBoot的web开发

SpringBoot的web开发

一、自动配置的ViewResolver

1.png

视图的配置mvcProperties对象中:
org.springframework.boot.autoconfigure.web.WebMvcProperties.View

2.png

二、自动配置静态资源

2.1 进入规则为/

如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

测试:

3.png

4.png

访问:127.0.0.1:8088/timg.jpg

2.2 进入规则为*.xxx 或者 不指定静态文件路径时

将静态资源放置到webapp下的static目录中即可通过地址访问:

5.png

测试:

6.png

三、自定义消息转化器

原来的(springmvc.xml中):

<!-- 注解驱动 -->
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg index="0" value="UTF-8" />
        </bean>
        <bean class="com.hcx.common.spring.extend.converter.json.CallbackMappingJackson2HttpMessageConverter">
            <property name="callbackName" value="callback" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

自定义消息转化器,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。

@Controller
@SpringBootApplication(exclude={RedisAutoConfiguration.class})  //排除redis的配置
@Configuration //声明该类为一个配置类
public class HelloApplication { 
//springboot的项目命名中,一般都有一个xxxApplication类,该类作为springboot项目的入口类

    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello world";
    }

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(HelloApplication.class);
        app.setBannerMode(Banner.Mode.OFF);//关闭banner
        app.run(args);
    }

}

默认配置:

7.png

8.png

四、自定义SpringMVC的配置

有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。

HelloApplication和MySpringMVCConfig在同一包下,所以在MySpringMVCConfig加入了@Configuration注解会被扫描进去,运行HelloApplication,会走该自定义拦截器。

MySpringMVCConfig:

package com.hcx.springboot.demo;

import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration //申明这是一个配置
public class MySpringMVCConfig extends WebMvcConfigurerAdapter{

    // 自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                System.out.println("自定义拦截器............");
                return true;
            }

            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                    ModelAndView modelAndView) throws Exception {

            }

            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                    Exception ex) throws Exception {
            }
        };
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    }

    // 自定义消息转化器的第二种方法
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }

}

HelloApplication:

package com.hcx.springboot.demo;

import java.nio.charset.Charset;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@SpringBootApplication(exclude={RedisAutoConfiguration.class})  //排除redis的配置
@Configuration //声明该类为一个配置类
public class HelloApplication { 
    //springboot的项目命名中,一般都有一个xxxApplication类,该类作为springboot项目的入口类

    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "hello world";
    }

    @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(HelloApplication.class);
        app.setBannerMode(Banner.Mode.OFF);//关闭banner
        app.run(args);
    }

}

注意:此时,有两个消息转换器;如果把两个消息转换器都去掉,spring中默认还有一个。把其中一个打开,则有一个,内部的是否创建取决于我们自己有没有创建,我们没有创建则内部的就会创建,如果我们有创建,则内部的不会创建。内部的是判断,容器中是否有,没有则创建,有就不创建了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个快速构建基于 Spring 框架的应用程序的工具。它为 Spring 应用程序开发提供了一种简单的方法,无需繁琐地配置 XML,只需要使用注解即可实现常见的业务逻辑。 下面是一个基本的 Spring Boot Web 应用程序的步骤: 1. 创建一个 Maven 项目,并添加 Spring Boot 的依赖。 2. 创建一个 Controller 类,并添加处理请求的方法。 3. 配置应用程序的入口点,并启动应用程序。 以下是一个简单的示例: 1. 创建 Maven 项目 使用 Maven 创建一个新的项目,可以参考以下命令: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 2. 添加 Spring Boot 依赖 在 pom.xml 文件中添加 Spring Boot Starter Web 依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 3. 创建 Controller 类 创建一个 HomeController 类,并添加处理请求的方法: ``` @RestController public class HomeController { @GetMapping("/") public String home() { return "Hello, World!"; } } ``` 4. 配置应用程序的入口点 创建一个 SpringBootWebApplication 类,并使用 @SpringBootApplication 注解标记为应用程序的入口点: ``` @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWebApplication.class, args); } } ``` 5. 启动应用程序 使用以下命令启动应用程序: ``` mvn spring-boot:run ``` 在浏览器中访问 http://localhost:8080/ ,即可看到 "Hello, World!"。 这就是一个简单的 Spring Boot Web 应用程序的开发过程。当然,除了以上步骤,还有很多其他的配置和实现方式,具体可以参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值