无法转换类型为';java.lang.String';至所需类型';ArrayList<;对象>';邮递
我正在寻找以下方面的可用用例:无法转换类型为';java.lang.String';至所需类型';ArrayList<;对象>';邮递,java,spring-boot,binding,type-conversion,Java,Spring Boot,Binding,Type Conversion,我正在寻找以下方面的可用用例: @PostMapping("/add-gateway") public String addGateway(ModelMap model, @Valid Gateway gateway, BindingResult result) { if (result.hasErrors()) { System.out.println("GatewayController has thrown error!"); return "ga
@PostMapping("/add-gateway")
public String addGateway(ModelMap model, @Valid Gateway gateway, BindingResult result) {
if (result.hasErrors()) {
System.out.println("GatewayController has thrown error!");
return "gateway";
}
service.addGateway(gateway.getId(), gateway.getGatewayName(), gateway.getIpAddress(), gateway.getDevices());
return "redirect:/list-gateways";
}
这是我的入门课程:
public class Gateway {
private String id;
private String gatewayName;
private String ipAddress;
private List<Device> devices;
@NotNull(message = "Empty gateway name not allowed!")
@Size(min = 5, message = "Enter at least 5 characters!")
public Gateway() {
super();
}
public Gateway(String id, String gatewayName, String ipAddress, List<Device> exampleDevices) {
super();
this.id = id;
this.gatewayName = gatewayName;
this.ipAddress = ipAddress;
this.devices = exampleDevices;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGatewayName() {
return gatewayName;
}
public void setGatewayName(String gwName) {
this.gatewayName = gwName;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public List<Device> getDevices() {
return devices;
}
public void setDevices(ArrayList<Device> devices) {
this.devices = devices;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Integer.parseInt(id);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Gateway other = (Gateway) obj;
if (id != other.id) {
return false;
}
return true;
}
public Map<String, List<Device>> getGatewayAndDevices(Gateway gateway) {
return (Map<String, List<Device>>) new HashMap().put(gateway.getGatewayName(), gateway.getDevices());
}
@Override
public String toString() {
return String.format("Gateway [id=%s, gatewayName=%s, ipAddress=%s, devices=%s]", id, gatewayName, ipAddress,
devices);
}
}
公共类网关{
私有字符串id;
私有字符串网关名称;
私有字符串IP地址;
私有列表设备;
@NotNull(消息=“不允许使用空网关名称!”)
@大小(最小值=5,消息=“输入至少5个字符!”)
公共网关(){
超级();
}
公共网关(字符串id、字符串网关名称、字符串IP地址、列表示例设备){
超级();
this.id=id;
this.gatewayName=网关名称;
this.ipAddress=ipAddress;
this.devices=示例设备;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
公共字符串getGatewayName(){
返回网关名称;
}
public void setGatewayName(字符串名称){
this.gatewayName=gwName;
}
公共字符串getIpAddress(){
返回IP地址;
}
public void setIpAddress(字符串ipAddress){
this.ipAddress=ipAddress;
}
公共列表getDevices(){
返回装置;
}
公共无效设置设备(ArrayList设备){
这个。设备=设备;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+Integer.parseInt(id);
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj){
返回true;
}
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
网关其他=(网关)obj;
if(id!=其他.id){
返回false;
}
返回true;
}
公共地图GetGateway和设备(网关){
return(Map)new HashMap().put(gateway.getGatewayName(),gateway.getDevices());
}
@凌驾
公共字符串toString(){
返回字符串.format(“网关[id=%s,网关名称=%s,ipAddress=%s,设备=%s]”,id,网关名称,ipAddress,
设备);
}
}
程序在返回的“gateway”字符串上失败,因为gateway构造函数的参数之一是对象的ArrayList,因此我从不向网关列表添加新网关,也从不返回“redirect:/list Gateways”。。。我尝试了很多变通方法来将ArrayList解析为字符串,或者使用转换器类,或者使用initBinder方法,但是它们在这里似乎都不起作用……我将非常感谢您的帮助,因为我需要一个有效的解决方案,直到明天结束!非常感谢 您可以共享网关类吗?这是你的课吗,我是说你能改一下吗?api的输入是什么?嗨,我在下面添加了我的网关类,我在你的问题中添加了你的代码,你能从答案中删除你的代码吗。