Spring MVC 开发记录

spring mvc 日志开启

1.log4j 配置修改logger.org.springframework.web日志级别,打印详细web 请求/响应日志信息
log4j配置:

log4j.logger.org.springframework.web=debug

logback配置:

<logger name="org.springframework.web" level="DEBUG" />

控制台响应显示:

[时间:2017-02-09 04:54:13] [级别:DEBUG] [类:org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor]  [消息:Written [{status=500, success=false, msg=验证码不正确,请重新输入}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@6e5ea41d]] 

这样在开发时候就不用每次去浏览器控制台查看,后台到底响应的什么数据内容。test updateddd

spring mvc get数组查询注意

如果是List 必须加@RequestParam否则无法注入

前端请求

在这里插入图片描述

后端写法

	// 可以接受请求
    @GetMapping("/testArray1")
    public String testArray1(String[] array) throws BusinessException {
        return MessageEnum.HELLO_ERROR.simpleInfo();
    }
    // 可以接受请求
    @GetMapping("/testArray11")
    public String testArray11(@RequestParam String[] array) throws BusinessException {
        return MessageEnum.HELLO_ERROR.simpleInfo();
    }
 
    // 不能接收请求
    @GetMapping("/testArray2")
    public String testArray2(List<String> list) throws BusinessException {
        return MessageEnum.HELLO_ERROR.simpleInfo();
    } 
   // 可以接受请求
    @GetMapping("/testArray22")
    public String testArray22(@RequestParam List<String> list) throws BusinessException {
        return MessageEnum.HELLO_ERROR.simpleInfo();
    }

log4j.properties常用配置

log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[时间\:%d{yyyy-MM-dd hh\:mm\:ss}] [级别\:%p] [类\:%c]  [消息\:%m] %n 

maven tomcat:run

maven插件配置

 <!--插件管理-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- tomcat7插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <uriEncoding>${project.build.sourceEncoding}</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

运行项目

tomcat7:run
在这里插入图片描述

启动报错注意

启动报错servlet-api 可能会冲突 修改scope为provided (注意可能有多个servlet, 全项目搜索)

        <!--ServletAPI依赖包-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

相关参考:https://blog.51cto.com/u_12203282/6000583

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值