当我访问GenericMockingForm的内容时,它为空
@Controller
@RequestMapping(value = "/**", produces = "application/json")
public class GenericMockingController {
@Autowired private GenericMockingService genericMockingService;
@ResponseBody
@PostMapping
public ResponseEntity<String> processRequest(@RequestBody GenericMockingForm genericMockingForm, HttpServletRequest request) throws Exception {
String requestURI = request.getRequestURI();
return new ResponseEntity<>(genericMockingService.getJsonResponse(genericMockingForm, requestURI), HttpStatus.OK);
}
}
public class GenericMockingForm {
private List<String> productIds;
private String salesChannelId;
private Locale locale;
public List<String> getProductIds() {
return productIds;
}
public void setProductIds(List<String> productIds) {
this.productIds = productIds;
}
public String getSalesChannelId() {
return salesChannelId;
}
public void setSalesChannelId(String salesChannelId) {
this.salesChannelId = salesChannelId;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
}
test payload and ulr http://localhost:8088/product/get-products:
{
"product_ids": ["1420-15595-82", "1420-58134-82"],
"sales_channel_id": "www-k24-at",
"locale": "de_DE"
}
以上是我的代码
如何反序列化请求有效负载? 当我访问GenericMockingForm的内容时,它为null
java.lang.NullPointerException
at com.kfz24.mockingservice.service.impl.GernericMockingServiceImpl.getJsonResponse(GernericMockingServiceImpl.java:32)
at com.kfz24.mockingservice.controller.GenericMockingController.processRequest(GenericMockingController.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
答案 0 :(得分:1)
您的测试有效载荷应为:
{
"productIds": ["1420-15595-82", "1420-58134-82"],
"salesChannelId": "www-k24-at",
"locale": "de_DE"
}
答案 1 :(得分:1)
好的。我通过更改POJO的结构解决了该问题
public class GenericMockingForm {
@JsonProperty("product_ids")
private List<String> productIds;
@JsonProperty("sales_channel_id")
private String salesChannelId;
@JsonProperty("locale")
private Locale locale;
public List<String> getProductIds() {
return productIds;
}
public void setProductIds(List<String> productIds) {
this.productIds = productIds;
}
public String getSalesChannelId() {
return salesChannelId;
}
public void setSalesChannelId(String salesChannelId) {
this.salesChannelId = salesChannelId;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
}