一、获取到Nginx默认反向后的端口为80导致请求失败

自己之前在项目中遇到这个问题记录一下,顺便补一下Nginx的知识点:

Request.getServerPort() nginx 获取错误

        Nginx默认反向后的端口为80,因此存在被代理后的端口为80的问题,这就导致访问出错。主要原因在Nginx的配置文件的host配置时没有设置响应的端口。

相关配置文件如下:

proxy_pass http://ime-server/ime-server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        如上,Host配置只有host,没有对应的port,这就导致在被代理的地方取得错误的端口。本文以java为例:

proxy_pass http://ime-server/ime-server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        这时,取得的port为80,虽然nginx监听的端口为9090。

        这个错误让我很郁闷。于是,修改nginx的配置文件,将Host后面的改为 $host:$server_port即可,配置文件如下:

location /ime-server {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://ime-server/ime-server;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

 重启nginx,./nginx -s reload 。然后检查被代理后的端口信息是否正确:

iOS 请求参数太长 请求参数失败_字段

二、Get请求上传文件一直获取不到参数问题:

原因:缺失@RequestParam导致获取不到参数。

springMVC中@RequestParam和@RequestBody注解的用法如下:

转载原文链接:springMVC中@RequestParam和@RequestBody注解的用法 - PC君 - 博客园

springMVC中@RequestParam注解用在Controller层获解析、提取参数,当然你也可以用request.getParameter("name")来获取参数,而@RequestParam注解接收参数有几种不同的写法。

1、test(String name)

像正常的方法接收参数,不带@RequestParam注解。这种写法,如果没有name参数不会报错。

2、test(@RequestParam String name)

带@RequestParam注解。这种写法,name参数为必须,如果没有会报错。

3、test(@RequestParam("userName") String name)

带@RequestParam注解。这种写法,可以对接收的参数进行重命名。即接收到userName改成name。

4、test(@RequestParam(value="userName",request=false) String name)

带@RequestParam注解。这种写法,通过设置request参数来设定参数是否为必须,true为必须,false为非必须。

PS:@RequestBody注解也可以用来获取参数。与@RequestParam不同点在于:

@RequestParam注解通常处理content-type为默认的application/x-www-form-urlcoded的请求。

@RequestBody注解用来处理content-type为application/json或者是application/xml等。

比如:一个json格式的请求参数,且没有参数名,就可以用下面的方式获取。

1、test(@RequestBody String json)

三、简单总结部分注解的使用:

1.@RequsetBody 和 @RequestParam 的区别:

@RequestParam注解通常处理content-type为默认的application/x-www-form-urlcoded的请求。

1、test(String name)
像正常的方法接收参数,不带@RequestParam注解。这种写法,如果没有name参数不会报错。

2、test(@RequestParam String name)
带@RequestParam注解。这种写法,name参数为必须,如果没有会报错。

3、test(@RequestParam("userName") String name)
带@RequestParam注解。这种写法,可以对接收的参数进行重命名。即接收到userName改成name。

4、test(@RequestParam(value="userName",request=false) String name)
带@RequestParam注解。这种写法,通过设置request参数来设定参数是否为必须,true为必须,false为非必须。

@RequestBody注解用来处理content-type为application/json或者是application/xml等。

比如:一个json格式的请求参数,且没有参数名,就可以用下面的方式获取。

1、test(@RequestBody String json)

2.@Param和@RequestParam是什么关系呢?

        其实它们没有关系,就跟Java和JavaScript,雷锋和雷锋塔一样,拥有相似的外表,其实作用是不一样的。

        @Param是地处Dao层,是为了传递多个参数,解决的是可读性和直观性;   

        @RequestParam是位列Controller层,作用是为获取前端参数,解决的是前后端参数不一致的问题。所以它们没有关系!

        注意:POST请求的时候一般需要加该注解,不然有的时候会无法获取到请求体中的参数,导致后端一直没法获取到前端传递的参数。   

3.MyBatis 使用@Param("XXX")注解传参和不使用的区别:

区别是:使用注解的时候,在xml文件中可以不用加parameterType

public interface SystemParameterMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(SystemParameterDO record);

    SystemParameterDO selectByPrimaryKey(Integer id);//不使用注解

    List<SystemParameterDO> selectAll();

    int updateByPrimaryKey(SystemParameterDO record);

    SystemParameterDO getByParamID(@Param("paramID") String paramID);//使用注解
}

跟映射的xml

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select id, paramID, paramContent, paramType, memo
    from wh_system_parameter
    where id = #{id,jdbcType=INTEGER}
  </select>

<select id="getByParamID"  resultMap="BaseResultMap">
    select id, paramID, paramContent, paramType, memo
    from wh_system_parameter
    where paramID = #{paramID}
  </select>

4.@JsonFormat

注意:

jackson在序列化时间时是按照国际标准时间GMT进行格式化的,而在国内默认时区使用的是CST时区,两者相差8小时。

5.Spring-注解@ActiveProfiles和@Profile


@ActiveProfiles
一般声明在UT测试类上,用于指定这个测试类里的测试方式运行时的profiles

@Profile
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件

  1. 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
  2. 写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效

6.@EnableScheduling 、@Scheduled、@Async

6.1SpringBoot定时任务@EnableScheduling

6.2@EnableScheduling和@Scheduled的使用

6.3 注解@Async异步线程池用

Spring中@Async用法总结

7. Spring中@JsonInclude

        @JsonInclude(JsonInclude.NON_NULL)的作用:jackson实体转为json为null的字段不参加序列化(即不显示)

        后端返回给前端JSON格式的对象数据中,当对象的字段为NULL时,该字段也会写入JSON返回;而很多时候前端期望后端只返回对象中非null的字段数据。在Jackson框架中提供了 @JsonInclude 注解以实现该功能

7.1不返回null字段数据

在相关对象的类上添加 @JsonInclude 注解,设定值为 NON_NULL

@JsonInclude(JsonInclude.Include.NON_NULL)
    public class Student
    {
        private int id;
        private String username;
        private String sex;
        private String address;
        ...
    }

则HTTP Response返回的该类的对象的JSON数据如下所示,无为null的字段

{
        "id": 0,
        "username": "Kallen",
        "sex": "female"
    }

7.2返回null字段数据

在相关对象的类上添加 @JsonInclude 注解,设定值为 ALWAYS

@JsonInclude(JsonInclude.Include.ALWAYS)

则HTTP Response返回的该类的对象的JSON数据如下所示,其中包含null的字段

{
        "id": 0,
        "username": "Kallen",
        "sex": "female",
        "address": null
    }
  • 该注解不设定值的情况下,默认使用 ALWAYS