使用Spring进行数据提交时出现空值。

huangapple go评论75阅读模式
英文:

Getting nulls when trying to post data using spring

问题

我使用 Spring 框架创建了一个使用 AJAX 进行 CRUD 操作的 REST 应用程序。问题是:当我从客户端发送 POST 请求到服务器时,我在服务器端收到了一个属性全部为 null 的对象,而在客户端我得到的是状态码 200。以下是我的代码:

Product 类

public class Product {//由 Eclipse 生成的 getters、setters 和 toString()
     private Integer id;
     private String name;
     private String description;
     private String createdDate;
     private Integer placeStorage;
     private Boolean reserved;

REST 控制器

@RestController
public class ProductRestController {
    
    @Autowired
    private ProductServiceImpl productService;
    
    @PostMapping(value = "/getRecord")
    public void addProduct(Product product) {
        System.out.println(product);
    }
}

AJAX 函数

function sendRecord(data){
    $.ajax({
        type : 'POST',
        url : '/getRecord',
        contentType : "application/json",
        cache : false,
        dataType: 'json',
        processData:false,
        data: data,
        dataType:'json',
        success : function(data) {
            update_table();
        }
    });
}

我发送的数据

使用Spring进行数据提交时出现空值。

我收到的数据

使用Spring进行数据提交时出现空值。

英文:

I have made a crud ajax rest application using spring framework. The problem is:
When i am sending post from client to server, i am getting object filled with nulls, from client side i have status 200. Here is my code:

Product class

public class Product {//getters,setters and toString() generated with Eclipse
	 private Integer id;
	 private String name;
	 private String description;
	 private String createdDate;
	 private Integer placeStorage;
	 private Boolean reserved;

Rest controller

@RestController
public class ProductRestController {
	
	@Autowired
	private ProductServiceImpl productService;
	
	@PostMapping(value = "/getRecord")
	public void addProduct(Product product) {
		System.out.println(product);
	}
	
}

AJAX function

function sendRecord(data){
		$.ajax({
			type : 'POST',
			url : '/getRecord',
			contentType : "application/json",
			cache : false,
			dataType: 'json',
			processData:false,
			data: data,
			dataType:'json',
			success : function(data) {
				update_table();
			}
		});
	}

Data that i am posting

使用Spring进行数据提交时出现空值。

Data that i am getting

使用Spring进行数据提交时出现空值。

答案1

得分: 2

你没有告诉 Spring 从哪里获取你的产品。你缺少一个注解。

@RestController
public class ProductRestController {
    
    @Autowired
    private ProductServiceImpl productService;
    
    @PostMapping(value = "/getRecord")
    public void addProduct(@RequestBody Product product) {
        System.out.println(product);
    }
    
}

你还应该考虑将 /getRecord 重命名,因为你并没有获取任何东西,而是在发送一些内容。 使用Spring进行数据提交时出现空值。

英文:

You don't tell Spring where to get your product from. You miss an annotation.

@RestController
public class ProductRestController {
    
    @Autowired
    private ProductServiceImpl productService;
    
    @PostMapping(value = "/getRecord")
    public void addProduct(@RequestBody Product product) {
        System.out.println(product);
    }
    
}

You should also think about rename /getRecord since you don't get anything but send something. 使用Spring进行数据提交时出现空值。

答案2

得分: 1

查看一下 @RequestBody 注解,链接:https://www.baeldung.com/spring-request-response-body。

@PostMapping(value = "/getRecord")
    public void addProduct(@RequestBody Product product) {
        System.out.println(product);
    }

它会自动将请求体数据映射到你的 Product 对象中。如果你有时间,还可以查看一下 @RequestParam@PathVariable,它们都用于获取数据,并且针对不同的目标以不同的方式工作。

英文:

Take a look into @RequestBody annotation https://www.baeldung.com/spring-request-response-body.

@PostMapping(value = "/getRecord")
    public void addProduct(@RequestBody Product product) {
        System.out.println(product);
    }

It will automatically map request body data into your Product object. If you got time also take a look into @RequestParam and @PathVariable, they are all for data getting and works in differents ways for different goals.

huangapple
  • 本文由 发表于 2020年9月11日 18:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63845714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定