- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在尝试将 JSON 请求直接映射到 XML 定义。我的想法是,我有一个使用 SOAP 服务并将其公开为 RESTful 服务的 Spring Boot 应用程序;
下面是 SOAP 绑定(bind)类定义;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"log",
"pass",
"idNumber",
"serialNumber"
})
@XmlRootElement(name = "GetDataByIdCard")
public class GetDataByIdCard {
@XmlElementRef(name = "log", namespace = "http://tempuri.org/", type = JAXBElement.class, required = false)
protected JAXBElement<String> log;
@XmlElementRef(name = "pass", namespace = "http://tempuri.org/", type = JAXBElement.class, required = false)
protected JAXBElement<String> pass;
@XmlElementRef(name = "id_number", namespace = "http://tempuri.org/", type = JAXBElement.class, required = false)
protected JAXBElement<String> idNumber;
@XmlElementRef(name = "serial_number", namespace = "http://tempuri.org/", type = JAXBElement.class, required = false)
protected JAXBElement<String> serialNumber;
/**
* Gets the value of the log property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getLog() {
return log;
}
/**
* Sets the value of the log property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setLog(JAXBElement<String> value) {
this.log = value;
}
/**
* Gets the value of the pass property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getPass() {
return pass;
}
/**
* Sets the value of the pass property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setPass(JAXBElement<String> value) {
this.pass = value;
}
/**
* Gets the value of the idNumber property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getIdNumber() {
return idNumber;
}
/**
* Sets the value of the idNumber property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setIdNumber(JAXBElement<String> value) {
this.idNumber = value;
}
/**
* Gets the value of the serialNumber property.
*
* @return
* possible object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public JAXBElement<String> getSerialNumber() {
return serialNumber;
}
/**
* Sets the value of the serialNumber property.
*
* @param value
* allowed object is
* {@link JAXBElement }{@code <}{@link String }{@code >}
*
*/
public void setSerialNumber(JAXBElement<String> value) {
this.serialNumber = value;
}
}
下面是我的 Controller ;
@Autowired
private IPRSClient client;
@PostMapping("/getbyId")
public GetDataByIdCardResponse invokeGetbyIdCard(@RequestBody GetDataByIdCard IdCardRequest) {
return client.getById(IdCardRequest);
}
当我执行发布请求时,我收到以下错误日志;
2019-11-13 08:51:12.829 ERROR 21148 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : 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 javax.xml.bind.JAXBElement]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.xml.bind.JAXBElement` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('RES')
at [Source: (PushbackInputStream); line: 2, column: 8] (through reference chain: com.iprs.soap.api.bindingclasses.GetDataByIdCard["log"])] with root cause
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.xml.bind.JAXBElement` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('RES')
at [Source: (PushbackInputStream); line: 2, column: 8] (through reference chain: com.iprs.soap.api.bindingclasses.GetDataByIdCard["log"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1589) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1055) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1373) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:171) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202) ~[jackson-databind-2.10.0.jar:2.10.0]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3258) ~[jackson-databind-2.10.0.jar:2.10.0]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:205) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:158) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579) [tomcat-embed-core-9.0.27.jar:9.0.27]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.27.jar:9.0.27]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_221]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.27.jar:9.0.27]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_221]
我正在寻找的理想结果是对我的 RESTful 服务执行 Post 请求并将其映射到 XML 服务;当我尝试这样做时,JAXBElement
被证明是一个挑战。
编辑我尝试使用自己的自定义类进行映射,如下所示;
public class IdCardCredentials {
public IdCardCredentials() {
// TODO Auto-generated constructor stub
}
@JsonProperty("log")
private String log;
@JsonProperty("pass")
private String pass;
@JsonProperty("id_Number")
private String idNumber;
@JsonProperty("serialNumber")
private String serialNumber;
public String getLog() {
return log;
}
public void setLog(String log) {
this.log = log;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
}
我还将 Controller 更新为以下定义;
@SpringBootApplication
@RestController
public class IprsWsClientApplication {
@Autowired
private IPRSClient client;
@Autowired
private ModelMapper modelMapper;
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
@PostMapping("/getbyId")
public GetDataByIdCardResponse invokeGetbyIdCard(@RequestBody IdCardCredentials IdCardRequest) {
ObjectMapper mapper = new ObjectMapper();
System.out.println(IdCardRequest.getIdNumber() + "------ID NUMBER-----");
GetDataByIdCard IdcardReq = modelMapper.map(IdCardRequest, GetDataByIdCard.class);
System.out.println(IdcardReq.getIdNumber() + "------REAL ID NUMBER-----");
return client.getById(IdcardReq);
}
public static void main(String[] args) {
SpringApplication.run(IprsWsClientApplication.class, args);
}
}
但是映射器没有适本地分配值;也就是说所有值都为空。我正在寻找一种可以将值映射到此 SOAP 类定义的方法。
最佳答案
我想出了一个很长的解决这个问题的方法;我没有使用 ModelMapper
,而是改为 ObjectFactory
,如下所示;
@PostMapping("/getbyId")
public GetDataByIdCardResponse invokeGetbyIdCard(@RequestBody IdCardCredentials IdCardRequest) {
ObjectFactory factory = new ObjectFactory();
GetDataByIdCard IdcardReq = new GetDataByIdCard();
JAXBElement<String> idNo = factory.createGetDataByIdCardIdNumber(IdCardRequest.getIdNumber());
JAXBElement<String> log = factory.createGetDataByIdCardLog(IdCardRequest.getLog());
JAXBElement<String> pass = factory.createGetDataByIdCardPass(IdCardRequest.getPass());
IdcardReq.setLog(log);
IdcardReq.setPass(pass);
IdcardReq.setPass(idNo);
return client.getById(IdcardReq);
}
尽管它并不理想,但它确实有效。
关于java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException : Cannot construct instance of `javax.xml.bind.JAXBElement` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58831168/
我找不到关于 jackson 的ObjectMapper与其他映射器(例如dozer/mapStruct/modelMapping/etc)之间的区别的任何解释。所有文章都比较了dozer/mapSt
我正在使用Jackson来反序列化Kotlin数据类。我正在使用jackson-kotlin-module,但 jackson 却给我以下错误: Can not construct instance
我正在尝试将包从“com.fasterxml.jackson”重新定位到我自己的包(即“mypackage.com.fasterxml.jackson”),然后在我拥有的另一个 JAR 中使用它。 我
对于JSON对象,Subject: { "permissions":["foo", "bar"], ... } ...我想反序列化为: class Subject { priv
我正在使用 @JsonTypeInfo 和 @JsonSubTypes 来映射基于给定属性的解析子类。这是我想要解析的示例 JSON 的一个人为示例。 { "animals": [ { "
我们正在使用 dropwizard 版本 0.6.3。当我们尝试升级 0.7.0 版本时,我们在服务启动时收到此错误。 线程“main”中的异常 java.lang.VerifyError: clas
我正在尝试实现自定义解串器。因为我只想向默认反序列化器添加功能,所以我尝试在我的自定义反序列化器中存储默认反序列化器:我想使用默认反序列化 json,然后添加其他信息。 我正在尝试使用 BeanDes
我有一个这样的类(class): public class Person { private String name; public String getName(){ return
我有以下 Kotlin 数据类: data class TestObject( val boolField: Boolean, val stringField: Str
使用 Jackson 库,在 Eclipse 4.9.0 版本中出现以下错误 缺少工件 com.fasterxml.jackson.core:jackson-databind:bundle:2.9.6
我试图在我的应用程序中从azure实现keyvault,在为DefaultAzureCredentialBuilder()实现azure-identity:1.5.4 lib后,它会抛出链接错误,如下
我试图在我的应用程序中从azure实现keyvault,在为DefaultAzureCredentialBuilder()实现azure-identity:1.5.4 lib后,它会抛出链接错误,如下
我知道我们可以使用 Jackson MixIn 来重命名属性或忽略属性(参见示例 here )。但是可以添加属性吗? 添加的属性可以是: 一个常数(如版本号) 计算值(例如,如果源类具有 getWid
我有一个在 Wildfly 10 上运行的应用程序,它需要更新版本的 jackson。简单地更新 maven 依赖是行不通的。 Wildflys 自己的版本似乎干扰了... 有人有提示吗? 最佳答案
我在 Tomcat 休息应用程序中运行 Jersey 2.5.1 & Jackson。对于我最初将 POJO 简单地转换为 JSON 的用例,基本设置效果很好。集合很好地转换为 json 数组,如下所
有没有办法告诉 jackson 在序列化过程中忽略那些用非 jackson 注释注释的字段? 例如: @SomeAnnotation private String foo; 我知道有 jackson
我遇到了 jackson 序列化问题, map 中存在空值。显然,这是 Wildfly 9 使用的 Jackson 版本中的一个已知错误 ( https://issues.jboss.org/brow
给定一个像这样的 JSON 映射类: public class Person { @JsonProperty String getName() { ... } @JsonPro
如何让 Jackson 的 XMLMapper 在反序列化时读取根 xml 元素的名称? 我正在将输入 XML 反序列化为通用 Java 类、LinkedHashMap,然后反序列化为 JSON。我想
我对抽象类和 JSON 序列化和反序列化的对象引用有问题。抽象的问题如下所示: 我有一个由节点和边组成的图。每条边连接两个节点。节点可以是红色和绿色的。因此,有一个抽象类Node和两个派生类 RedN
我是一名优秀的程序员,十分优秀!