类型接口的Java Arraylist
我在弹簧启动中有两个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 ofcom.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是参数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")
我在这里猜测,因为您没有共享
parameterdto1
或parameterdto2
- 的实现,并且由于某种原因,您的接口被称为import metermetadata
,其中根据例外,您的说明和其他文件应该是参数Interface
。问题在于杰克逊(Jackson)将getfiles/setfiles视为属性,其类型是接口,您没有发送任何类型的信息。
通常,假设
parameterdto1
和parameterdto2
正在使用file> file> fileinterface
的concreate实现,您只需更改接口方法getfiles
/code>/setfiles
因此,他们使用generics参数,并且在每个实现中,设置了您正在使用的fileinterface
的混乱类型,这将使杰克逊能够理解file> fileinterface
fileinterface。
incase
parameterdto1
和parameterdto2
不使用file> fileinterface
的concreate实现,您应该添加@jsontypeinpeinfo
或@@@jsontypeinfo
@ jsonsubtypes (请参阅 https://www.baeldung.com/jackson-andoctation 第5节有关更多信息) - 请注意,调用API的客户端还应在JSON型字段中指定实际类型建议实现
im guessing here because you didnt share the implementation of
ParameterDto1
orParameterDto2
- and for some reason your interface is calledImportMetaData
where according to the exception, your explanation and other files it should beParameterInterface
.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
andParameterDto2
are using a concreate implementation ofFileInterface
you could just change your interface methodsgetFiles
/setFiles
so they are using generics parameter and in each implementation set the concreate type forFileInterface
you are using , this will allow jackson to understand the concreate type forFileInterface
.incase
ParameterDto1
andParameterDto2
are not using a concreate implementation ofFileInterface
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 fieldSuggested implementation