英文:
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();
}
});
}
我发送的数据
我收到的数据
英文:
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
Data that i am getting
答案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
重命名,因为你并没有获取任何东西,而是在发送一些内容。
英文:
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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论