无法使用Worklight适配器和wink/jackson restful发送/解析JSON
背景:我正在使用Worklight适配器使用restful服务无法使用Worklight适配器和wink/jackson restful发送/解析JSON,json,jackson,ibm-mobilefirst,worklight-adapters,apache-wink,Json,Jackson,Ibm Mobilefirst,Worklight Adapters,Apache Wink,背景:我正在使用Worklight适配器使用restful服务ApacheWink是我的restful实现servlet。我在我的服务中使用了jackson相关的jar 问题:如果我尝试使用表单urlencoded类型使用服务,它会运行平稳。然而,当我尝试使用JSON时,它失败了,从适配器发送的JSON字符串的第一个字符出现解析异常 相关实施和例外情况,见下文 使用服务的适配器代码(在worklight server中运行): Jackson注册()。即使我完全删除了这段代码,也会发生同样的
ApacheWink
是我的restful实现servlet。我在我的服务中使用了jackson
相关的jar
问题:如果我尝试使用表单urlencoded
类型使用服务,它会运行平稳。然而,当我尝试使用JSON
时,它失败了,从适配器发送的JSON字符串的第一个字符出现解析异常
相关实施和例外情况,见下文
使用服务的适配器代码(在worklight server中运行):
Jackson注册()。即使我完全删除了这段代码,也会发生同样的异常
public Set<Object> getSingletons() {
Set<Object> s = new HashSet<Object>();
// Register the Jackson provider for JSON
// Make (de)serializer use a subset of JAXB and (afterwards) Jackson annotations
// See http://wiki.fasterxml.com/JacksonJAXBAnnotations for more information
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector primary = new JaxbAnnotationIntrospector();//new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
mapper.getDeserializationConfig().setAnnotationIntrospector(pair);//setAnnotationIntrospector(pair);
mapper.getSerializationConfig().setAnnotationIntrospector(pair);
// Set up the provider
JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider();
jaxbProvider.setMapper(mapper);
s.add(jaxbProvider);
return s;
}
需要考虑的事项: 在适配器代码中,在参数
参数:{“commonname”:username,“password”:password}
中,第一个字符始终失败。这次是c
,因此它在c
中失败(代码99对应c
的ASCII
值)
如果它是:参数:{“username”:username,“password”:password}
,它将失败,出现u
,相应的代码更改为其ASCII
值,即117
我还尝试使用单引号而不是双引号、无引号和转义引号。每次在第一个字符上失败。我还尝试显式创建JSON并将其作为参数传递,如:
var credentials = JSON.stringify({'username': username, 'password': password});
parameters: {"credentials":credentials} //still fails in first char
//also as parameters: {credentials} :
//this fails in adapter itself for wrong type of parameter
我想的是:
由于解析失败,我认为我尝试从适配器发送JSON的方式不是正确的方式,或者我没有正确注册Jackson,因此它无法正确解析JSON。但我完全不知道从何处查找此问题
我真的很感谢你们大家花时间阅读这个长的问题和任何有用的输入。 <你的代码> JSON<代码>无效。
{
"commonname": "username",
"password": "password"
}
<代码> <代码>标记确实需要.< /p>您的代码> JSON<代码>无效。请考虑:<代码> {“公共名称”:“用户名”、“密码”:“密码”}。那些没有引号的用户名和密码实际上是变量。很抱歉,混淆了。复制代码时遗漏了这一部分。但是没有引号,这些变量不能使
JSON
有效。谢谢@MichałZiober。这就是问题所在。作为一个替代,创建了javascript对象,然后将其字符串化,现在它可以工作了。如果你有时间的话,请把答案贴出来,这样我就可以把它标为正确答案。我很高兴,我能帮上忙。
var credentials = JSON.stringify({'username': username, 'password': password});
parameters: {"credentials":credentials} //still fails in first char
//also as parameters: {credentials} :
//this fails in adapter itself for wrong type of parameter
{
"commonname": "username",
"password": "password"
}