类型接口的Java Arraylist

发布于 2025-01-29 10:35:51 字数 1686 浏览 3 评论 0原文

我在弹簧启动中有两个Java端点:

@PostMapping(path="/my-import-1")
@ResponseStatus(HttpStatus.OK)
public String myImport1(@Valid @RequestBody ParameterDto1 params) {
    return this.serviceImpl.import(params);
}

并且

@PostMapping(path="/my-import-2")
@ResponseStatus(HttpStatus.OK)
public String myImport2(@Valid @RequestBody ParameterDto2 params) {
    return this.serviceImpl.import(params);
}

两者都使用相同的服务进行导入,但其参数有一些差异。

我创建了Service的导入方法

@Override
public String import(ParameterInterface params) throws Exception {
   ...
}

和类似的参数接口

public interface ImportMetaData {
    public default ArrayList<FileInterface> getFiles() {
        return null;
    }
    public void setFiles(ArrayList<FileInterface> files);
}

实现此接口,我创建了两个parameterdto class(parameterdto1和parameterdto2)。 IDE显示一切都是正确的,也是我服务的开始工作,但是一旦我将请求发送到其中一个端点,我就会收到以下错误:

servlet.service()用于servlet [dispatcherservlet]的上下文 []抛出异常[请求处理失败;嵌套异常是 org.springframework.http.converter.httpmessageconversionexception: 类型定义错误:[简单类型,类 com.beo.services.myimportservice.rest.domain.dto.metadata.interfaces.parameterinerface]; 嵌套异常是 com.fasterxml.jackson.databind.exc.invaliddefinitionException:不能 构造实例 com.beo.services.myimportservice.rest.domain.dto.metadata.interfaces.parameterinerface (没有像默认构造函数一样的创建者):抽象类型要么 需要映射到混凝土类型,具有自定义求职者或 在[来源: (tublbackInputStream);线:3,列:5](通过参考链: com.beo.services.myimportservice.rest.domain.dto.metadata.parameterdto [“ files”] - &gt; java; java.util.arraylist [0])] 有根本原因

我可以如何从接口创建这样的ArrayList并使这两个端点运行?还是还有其他解决方案?

I have two java endpoints in spring boot like this:

@PostMapping(path="/my-import-1")
@ResponseStatus(HttpStatus.OK)
public String myImport1(@Valid @RequestBody ParameterDto1 params) {
    return this.serviceImpl.import(params);
}

and

@PostMapping(path="/my-import-2")
@ResponseStatus(HttpStatus.OK)
public String myImport2(@Valid @RequestBody ParameterDto2 params) {
    return this.serviceImpl.import(params);
}

Both use the same service for importing, but have some differences in their parameters.

I created the service's import method like this

@Override
public String import(ParameterInterface params) throws Exception {
   ...
}

and the ParameterInterface like this

public interface ImportMetaData {
    public default ArrayList<FileInterface> getFiles() {
        return null;
    }
    public void setFiles(ArrayList<FileInterface> files);
}

Implementing this interface I created two ParameterDto classes (ParameterDto1 and ParameterDto2). The IDE shows everything is correct, also the start of my service works, but as soon as I send a request to one of the endpoints, I get the following error:

Servlet.service() for servlet [dispatcherServlet] in context with path
[] threw exception [Request processing failed; nested exception is
org.springframework.http.converter.HttpMessageConversionException:
Type definition error: [simple type, class
com.beo.services.myImportService.rest.domain.dto.metadata.interfaces.ParameterInerface];
nested exception is
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of
com.beo.services.myImportService.rest.domain.dto.metadata.interfaces.ParameterInerface
(no Creators, like default constructor, exist): abstract types either
need to be mapped to concrete types, have custom deserializer, or
contain additional type information at [Source:
(PushbackInputStream); line: 3, column: 5] (through reference chain:
com.beo.services.myImportService.rest.domain.dto.metadata.ParameterDto["files"]->java.util.ArrayList[0])]
with root cause

Can I any how create such an ArrayList from an interface and get these two endpoints running? Or is there another solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他夏了夏天 2025-02-05 10:35:51

问题是参数DTO1和参数DTO2。 Jackson Library需要一个默认,NO-ARGS构造函数或带有@jsonproperty(“ Field_name”)的参数的构造函数,否则它无法转换您的消息。

解决方案:

将NO-ARGS构造函数添加到参数DTO1和参数DTO2或用@jsonproperty(“ Field_name”) andereTtto2或注释构造函数参数>

The issue is with the ParameterDto1 and ParameterDto2. Jackson library requires a default, no-args constructor or a constructor with parameters annotated with @JsonProperty("field_name"), otherwise it cannot convert your message.

Solution:

Add a no-args constructor to ParameterDto1 and ParameterDto2 or annotate the constructor parameters with @JsonProperty("field_name")

抽个烟儿 2025-02-05 10:35:51

我在这里猜测,因为您没有共享parameterdto1parameterdto2 - 的实现,并且由于某种原因,您的接口被称为import metermetadata,其中根据例外,您的说明和其他文件应该是参数Interface

问题在于杰克逊(Jackson)将getfiles/setfiles视为属性,其类型是接口,您没有发送任何类型的信息。

通常,假设parameterdto1parameterdto2正在使用file> file> fileinterface的concreate实现,您只需更改接口方法getfiles/code>/ setfiles因此,他们使用generics参数,并且在每个实现中,设置了您正在使用的fileinterface的混乱类型,这将使​​杰克逊能够理解file> fileinterface fileinterface

incase parameterdto1parameterdto2不使用file> fileinterface的concreate实现,您应该添加@jsontypeinpeinfo@@@jsontypeinfo@ jsonsubtypes (请参阅 https://www.baeldung.com/jackson-andoctation 第5节有关更多信息) - 请注意,调用API的客户端还应在JSON型字段中指定实际类型

建议实现

public interface ParameterInterface {
@JsonIgnore
public List<FileInterface> getParameters() default { return null;}
.....
}

public class ParameterDto1 implements ParameterInterface {
private List<FileImpl1> files;
public List<FileImpl1> getFiles(){return files;}
public void setFiles(List<FileImpl1> files){this.files=files;}
....
}



public class ParameterDto2 implements ParameterInterface {
private List<FileImpl2> files;
public List<FileImpl2> getFiles(){return files;}
public void setFiles(List<FileImpl2> files){this.files=files;}
...
}


public class FileImpl1 implements FileInterface{

...
}

public class FileImpl2 implements FileInterface{

...
}

im guessing here because you didnt share the implementation of ParameterDto1 or ParameterDto2 - and for some reason your interface is called ImportMetaData where according to the exception, your explanation and other files it should be ParameterInterface.

the problem is that getFiles/setFiles is considered as a property by jackson , its type is an interface and you are not sending any type information.

in general assuming ParameterDto1 and ParameterDto2are using a concreate implementation of FileInterface you could just change your interface methods getFiles/setFiles so they are using generics parameter and in each implementation set the concreate type for FileInterface you are using , this will allow jackson to understand the concreate type for FileInterface .

incase ParameterDto1 and ParameterDto2 are not using a concreate implementation of FileInterface you should add @JsonTypeInfo or @JsonSubTypes (see https://www.baeldung.com/jackson-annotations section 5 for more info) - note that the client calling the api should also specify the actual type in the json-type field

Suggested implementation

public interface ParameterInterface {
@JsonIgnore
public List<FileInterface> getParameters() default { return null;}
.....
}

public class ParameterDto1 implements ParameterInterface {
private List<FileImpl1> files;
public List<FileImpl1> getFiles(){return files;}
public void setFiles(List<FileImpl1> files){this.files=files;}
....
}



public class ParameterDto2 implements ParameterInterface {
private List<FileImpl2> files;
public List<FileImpl2> getFiles(){return files;}
public void setFiles(List<FileImpl2> files){this.files=files;}
...
}


public class FileImpl1 implements FileInterface{

...
}

public class FileImpl2 implements FileInterface{

...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文