ilh*_*han 8 java spring spring-boot
我试图将a设置List<Long>为Java对象。
设置功能为:
ResponseEntity<String> response = bcInsertService.addNewClip(new PrmBcClipInsert()
.setTags(Arrays.asList(new Long[]{5L, 3L}))
);
Run Code Online (Sandbox Code Playgroud)
而对象是
public class PrmBcClipInsert implements Serializable {
@ApiModelProperty(required = true)
private List<Long> tags;
public List<Long> getTags() {
return tags;
}
public PrmBcClipInsert setTags(List<Long> tags) {
this.tags = tags;
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
这是BcInsertService:
public class BcInsertService extends RestTemplate {
private static final Logger log = LoggerFactory.getLogger(BcInsertService.class);
public ResponseEntity<String> addNewClip(PrmBcClipInsert prmBcClipInsert) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map= new LinkedMultiValueMap<String, Object>();
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(prmBcClipInsert.getParameters(), headers);
ParameterizedTypeReference<StandardResponse> typeRef = new ParameterizedTypeReference<StandardResponse>() {};
ResponseEntity<String> response = this.postForEntity( "http://localhost:8080/bc/add-clip", request , String.class );
log.info(response.toString());
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
它返回一个错误:
字段“标签”上的对象“ prmBcClipInsert”中的字段错误:拒绝值[[5,3]];代码[typeMismatch.prmBcClipInsert.tags,typeMismatch.tags,typeMismatch.java.util.List,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[prmBcClipInsert.tags,tags]; 参数[]; 默认消息[标签]];默认消息[未能将类型'java.lang.String'的属性值转换为属性'tags'的必需类型'java.util.List';嵌套的异常是java.lang.NumberFormatException:对于输入字符串:“ [5,3]”]
为什么该方法即使说它接受一个列表也不接受列表?
我能够使用表单验证来重新创建您的错误情况。您可能正在尝试传递[5, 3]用于tags类型为的变量的表单数据List<Long>,但是使用方括号传递会破坏该结构,该值应为5, 3...
所以我要做的是;
使用您的输入创建一个虚拟控制器;
@Controller
public class TestController {
@PostMapping
public ModelAndView test(@Validated @ModelAttribute final PrmBcClipInsert prmBcClipInsert, final BindingResult bindingResult) {
final ModelAndView modelAndView = new ModelAndView();
System.out.println(prmBcClipInsert.getTags());
modelAndView.setViewName("test");
return modelAndView;
}
}
Run Code Online (Sandbox Code Playgroud)用传递表单tags=[5,3],并在中出现以下错误BindingResult;
org.springframework.validation.BeanPropertyBindingResult:1个错误字段'tags'上的对象'prmBcClipInsert'中的字段错误:拒绝的值[[5,3]]; 代码[typeMismatch.prmBcClipInsert.tags,typeMismatch.tags,typeMismatch.java.util.List,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[prmBcClipInsert.tags,tags]; 参数[]; 默认消息[标签]];默认消息[未能将类型'java.lang.String'的属性值转换为属性'tags'的必需类型'java.util.List';嵌套的异常是java.lang.NumberFormatException:对于输入字符串:“ [5,3]”]
这是与您得到的错误相同的错误...所以我想您要么PrmBcClipInsert像在我的示例中那样将其作为表单输入获取,要么您试图在代码的其他部分进行类似的绑定...
通过传递表格tags=5,3,没有错误 ...
可以有一个自定义转换器,以支持在绑定逻辑中将带有括号的数组输入传递给类似对象;例如:
@Component
public class LongListConverter implements Converter<String, List<Long>> {
@Override
public List<Long> convert(String source) {
return Arrays.stream(StringUtils.strip(source, "[]").split(","))
.map(StringUtils::strip)
.map(Long::new)
.collect(Collectors.toList());
}
}
Run Code Online (Sandbox Code Playgroud)
这样,两个5, 3&[5, 3]都可以作为tags变量的值提供。
| 归档时间: |
|
| 查看次数: |
623 次 |
| 最近记录: |