【发布时间】:2018-10-27 18:41:03
【问题描述】:
我有一个 JavaFX 客户端,它使用在 Spring Boot v2.0.0 中实现的 Rest 服务,在开发时我确实创建了一个控制器,其中它有一个 POJO 列表作为 Post 请求中的请求参数,所以我可以添加一组一次调用中的实体对象
这是我到目前为止所做的事情
我的控制器:
@PostMapping
private ServerResponse creatAllCompDatas(
@RequestParam final List<ComponentDataForm> componentDataFormValues
) {
if (isSessionValid()) {
List<Long> datasIds = new ArrayList();
componentDataFormValues.stream().forEach(formValue -> {
MNG_COMPOSANT_DATA mng_composant_data = new MNG_COMPOSANT_DATA();
mng_composant_data.setCmp_attr_code(formValue.getAttCode());
mng_composant_data.setCmp_attr_label(formValue.getAttTitle());
mng_composant_data.setCmp_attr_value(formValue.getAttVal());
datasIds.add(mng_composant_data.getId());
});
initSuccessResponse(datasIds);
return serverResponse;
}
initFailLoginResponse();
return serverResponse;
}
我的 ComponentDataForm POJO(本例中为 DTO)
@Component
public class ComponentDataForm {
@JsonProperty("attCode")
private String attCode;
@JsonProperty("attTitle")
private String attTitle;
@JsonProperty("attVal")
private String attVal;
public ComponentDataForm() {
}
public String getAttCode() {
return attCode;
}
public void setAttCode(String attCode) {
this.attCode = attCode;
}
public String getAttTitle() {
return attTitle;
}
public void setAttTitle(String attTitle) {
this.attTitle = attTitle;
}
public String getAttVal() {
return attVal;
}
public void setAttVal(String attVal) {
this.attVal = attVal;
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.attCode);
hash = 29 * hash + Objects.hashCode(this.attTitle);
hash = 29 * hash + Objects.hashCode(this.attVal);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ComponentDataForm other = (ComponentDataForm) obj;
if (!Objects.equals(this.attCode, other.attCode)) {
return false;
}
if (!Objects.equals(this.attTitle, other.attTitle)) {
return false;
}
if (!Objects.equals(this.attVal, other.attVal)) {
return false;
}
return true;
}
@Override
public String toString() {
return "ComponentDataForm{" + "attCode=" + attCode + ", attTitle=" + attTitle + ", attVal=" + attVal + '}';
}
public ComponentDataForm(String attCode, String attTitle, String attVal) {
this.attCode = attCode;
this.attTitle = attTitle;
this.attVal = attVal;
}
}
通过这个我恢复了我的后端
现在我将为您提供我的前端 Javafx:
@FXML
private void doCommit(ActionEvent event) throws UnirestException{
if(validate()){
final ObservableList<AppDataAttTDO> items = tableAtributes.getItems();
final List<ComponentDataForm> componentDataFormValues = new ArrayList();
items.stream().forEach(item -> {
final ComponentDataForm requestItem = new ComponentDataForm();
requestItem.setAttCode(item.getAttCode());
requestItem.setAttTitle(item.getAttTitle());
requestItem.setAttVal(item.getAttValue());
componentDataFormValues.add(requestItem);
});
HttpResponse<String> asString = Unirest.post(APPLICATION_DATA_SERVICE_URL)
.queryString("componentDataFormValues", componentDataFormValues)
.asString();
System.out.println(asString.getStatus());
System.out.println(asString.getBody());
}
}
当我将客户端的请求提交到服务器时,服务器会抛出以下异常
2018-05-17 14:47:24.970 WARN 3660 --- [nio-8443-exec-7] .wsmsDefaultHandlerExceptionResolver:无法转换请求元素:org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException:失败将“java.lang.String[]”类型的值转换为所需的“java.util.List”类型;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“com.easyData.pos.easyPos.rest.contoller.application.ComponentDataForm”:找不到匹配的编辑器或转换策略
2018-05-17 14:47:24.970 WARN 3660 --- [nio-8443-exec-7] .wsmsDefaultHandlerExceptionResolver:已解决由处理程序执行引起的异常:org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException:无法将类型“java.lang.String[]”的值转换为所需类型“java.util.List”;嵌套异常是 java.lang.IllegalStateException:无法将类型“java.lang.String”的值转换为所需类型“com.easyData.pos.easyPos.rest.contoller.application.ComponentDataForm”:找不到匹配的编辑器或转换策略
经过 1 天的研究,我确实不明白我需要创建一个转换器,通过扩展 org.springframework.core.convert.converter.Converter; 接口来创建一个 String、ComponentDataForm 转换器,它将接收到的请求参数解析为我的 POJO
但在这里我被困住了,我不知道如何编写这个转换器。
【问题讨论】:
标签: java spring rest spring-boot converter