无脑解决SpringBoot+json+postman post时候传参null

SpringBoot+json+postman post时候传参null

这家伙,我搞了两天。 get的时候啥事没有,post就出现这傻* 样子,特么的给我气炸了 快。

问题

==>  Preparing: INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept) VALUES (?,?,?,?,?)
==> Parameters: 110(String), 孙悟空(String), 男(String), 100(String), 10010(String)
<==    Updates: 1

然后我发现了这篇文章springboot post接口接受json时,转换为对象时,属性都为null
我真的太喜欢会读底层源码的银儿了。我理解的就是springboot默认的HttpMessageConverter解析jason时,用的转换器是MappingJackson2HttpMessageConverter。命名策略变为下划线+小写风格。

解决方法

1. 添加依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>

2.修改 yml文件

在这里插入图片描述

jackson:
  property-naming-strategy: LOWER_CAMEL_CASE

3.将springboot默认的HttpMessageConverter替换为阿里的FastJson转换器

在这里插入图片描述

package com.example.wangyue;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * Created by conan on 2019/1/7.
 */
@Configuration
public class MyWebmvcConfiguration extends WebMvcConfigurationSupport {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
    {
        //调用父类配置
        super.configureMessageConverters(converters);
        //创建消息转换器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //创建配置类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //将fastjson添加到视图消息转换器列表内
        converters.add(fastConverter);
    }
}

然后再次尝试,艾玛,快乐快乐~好了
在这里插入图片描述
在这里插入图片描述

复盘

虽然解决了问题,但是基本原来,还是不懂。所以完成这个小demo之后,要好好读源码。加油了!

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用SpringBootSpring Data JPA连接MySQL数据库并执行增删改查操作的示例代码: 1.在pom.xml文件中添加MySQL和Spring Data JPA依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ``` 2.在application.properties文件中配置MySQL数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update ``` 3.创建实体类User.java: ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String username; @Column(nullable = false) private String password; // 省略getter和setter方法 } ``` 4.创建Repository接口UserRepository.java: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); } ``` 5.创建Controller类UserController.java: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserRepository userRepository; @PostMapping("/add") public User addUser(@RequestBody User user) { return userRepository.save(user); } @GetMapping("/get/{id}") public User getUser(@PathVariable Long id) { return userRepository.findById(id).orElse(null); } @PutMapping("/update") public User updateUser(@RequestBody User user) { return userRepository.save(user); } @DeleteMapping("/delete/{id}") public void deleteUser(@PathVariable Long id) { userRepository.deleteById(id); } } ``` 6.使用Postman等工具测试接口,例如添加用户: 请求方式:POST 请求URL:http://localhost:8080/user/add 请求参数: ```json { "username": "test", "password": "123456" } ``` 返回结果: ```json { "id": 1, "username": "test", "password": "123456" } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值