泽西休息服务的 JSON 输入/输出在 JavaScript 中不可用
JSON input/output of a jersey rest service unsable in javascript
我喜欢用xml和json输入和输出构建一个休息服务。xml 输出应有效,并使用命名空间在 xsd 架构中定义。使用 xml 服务运行良好。对于 json,我没有找到实现一致输入和输出的解决方案。为了说明我的问题,我做了一个小例子。我创建了两个带有jaxb注释的类父和子。父对象具有子对象的@XmlAttribute和列表。其余服务可以生成两个示例父对象。一个示例包含一个子项,另一个示例包含两个子项。因为具有一个或多个成员的列表的表示法之间存在一些差异。此外,该服务还有一个@POST方法,该方法接收父对象并直接返回它。我的假设是将生成的 json 文本放入 post 方法中,并获得与以前相同的符号。但这在大多数情况下不起作用。
我的休息服务:
@Path("parent")
public class ParentResource {
@GET
@Produces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Parent generateExample(){
Parent father = new Parent();
father.setName("peter");
Child john = new Child();
john.setName("john");
father.getChildren().add(john);
return father;
}
@GET
@Path("list")
@Produces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Parent generateExample2(){
Parent father = new Parent();
father.setName("peter");
Child john = new Child();
john.setName("john");
Child sue = new Child();
sue.setName("sue");
father.getChildren().add(john);
father.getChildren().add(sue);
return father;
}
@POST
@Produces ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes ({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Parent echoEntity(Parent input){
return input;
}
}
父母
@XmlAccessorType (XmlAccessType.NONE)
@XmlRootElement (name="parent")
public class Parent implements Serializable{
@XmlAttribute
private String name;
@XmlElement
private List<Child> children = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Child> getChildren() {
if(children == null){
children = new LinkedList<Child>();
}
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
}
孩子
@XmlAccessorType (XmlAccessType.NONE)
@XmlRootElement (name="child")
public class Child implements Serializable {
@XmlElement
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
包信息.java在父包+子包中定义命名空间。
@javax.xml.bind.annotation.XmlSchema(namespace="http://myexample.com",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED)
自定义上下文解析程序
@Provider
@Produces (MediaType.APPLICATION_JSON)
@Consumes (MediaType.APPLICATION_JSON)
public class CustomResolver implements ContextResolver<JAXBContext>{
private final JAXBContext context;
private final Set<Class<?>> types;
private final Class<?>[] cTypes = {
Parent.class, Child.class
};
public CustomResolver() throws Exception {
this.types = new HashSet<Class<?>>(Arrays.asList(cTypes));
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("http://myexample.com", "ex");
JSONConfiguration config = JSONConfiguration.natural().rootUnwrapping(false).build();
// JSONConfiguration config = JSONConfiguration.mapped().xml2JsonNs(nsMap).attributeAsElement("name").rootUnwrapping(false).build();
// JSONConfiguration config = JSONConfiguration.mapped().attributeAsElement("name").rootUnwrapping(false).build();
// JSONConfiguration config = JSONConfiguration.mappedJettison().build();
// JSONConfiguration config = JSONConfiguration.badgerFish().build();
context = new JSONJAXBContext(config, cTypes);
}
public JAXBContext getContext(Class<?> objectType) {
return (types.contains(objectType)) ? context : null;
}
}
包括自定义上下文解析程序的 rest 应用程序
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public RestApplication(){
singletons.add(new ParentResource());
classes.add(CustomResolver.class);
}
@Override
public Set<Class<?>> getClasses(){
return classes;
}
@Override
public Set<Object> getSingletons(){
return singletons;
}
}
没有自定义上下文解析程序的测试结果(应用程序中的注释):
generated 1 :
{"@name":"peter","children":{"name":"john"}}
output of post method :
{"@name":"peter"}
generated 2 :
{"@name":"peter","children":[{"name":"john"},{"name":"sue"}]}
output of post method :
{"@name":"peter"}
使用自然符号的测试结果(我喜欢使用这种符号)
generated 1 :
{"parent":{"name":"peter","children":[{"name":"john"}]}}
output of post method :
Exception: The request sent by the client was syntactically incorrect ()
generated 2 :
{"parent":{"name":"peter","children":[{"name":"john"},{"name":"sue"}]}}
output of post method :
Exception: The request sent by the client was syntactically incorrect ()
具有映射表示法且无命名空间映射的测试结果
generated 1 :
{"parent":{"name":"peter","children":{"name":"john"}}}
output of post method :
The request sent by the client was syntactically incorrect ()
generated 2 :
{"parent":{"name":"peter","children":[{"name":"john"},{"name":"sue"}]}}
output of post method :
The request sent by the client was syntactically incorrect ()
使用映射符号的测试结果
generated 1 :
{"ex.parent":{"name":"peter","ex.children":{"ex.name":"john"}}}
output of post method :
{"ex.parent":{"name":"peter","ex.children":{"ex.name":"john"}}}
generated 2 :
{"ex.parent":{"name":"peter","ex.children":[{"ex.name":"john"},{"ex.name":"sue"}]}}
output of post method :
{"ex.parent":{"name":"peter","ex.children":[{"ex.name":"john"},{"ex.name":"sue"}]}}
哇,它有效。但是这种表示法我不喜欢在javascript中使用。命名空间前缀将合并到属性名称中。在访问嵌套元素时,这对于在 javascript 客户端中对前缀进行硬编码不是很有用。前缀名称可能会更改!使用抛弃符号,它看起来比之前的例子更糟糕。
说来话长,但一个简单的问题。是否有人提供解决方案来接收 json 自然表示法并能够将相同的表示法发送到球衣休息服务?
我通过更新到较新的球衣版本来管理它。现在我在 1.13 很旧之前使用 1.1.5.1。我也必须做同样的改变。在自定义上下文解析器中,我删除了注释@Consumes (MediaType.APPLICATION_JSON) 并添加一个新类并将其添加到应用程序类中的类中
@Provider
@Consumes (MediaType.APPLICATION_JSON)
public class NaturalJsonReader implements MessageBodyReader{
private static final JSONConfiguration jConfig = JSONConfiguration.natural().rootUnwrapping(false).build();
@Override
public boolean isReadable(Class arg0, Type type, Annotation[] arg2, MediaType mt) {
return true;
}
@Override
public Object readFrom(Class arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap arg4, InputStream arg5) throws IOException, WebApplicationException {
try {
JSONUnmarshaller m = new JSONJAXBContext(jConfig, arg0).createJSONUnmarshaller();
Object o = m.unmarshalJAXBElementFromJSON(arg5, arg0).getValue();
return o;
} catch (JAXBException e) {
}
return null;
}
}
在此之后,自然符号可用于其余服务的输入和输出操作。
- console.log以外的Javascript输出函数
- 比较两个值时出现JavaScript输出和控制流错误
- 监视JavaScript输出,例如console.log()
- 意外的javascript输出
- 获取表格格式的 Javascript 输出
- 在 JavaScript 输出中设置字体样式
- javascript输出true中的hasOwnProperty方法
- 让我的javascript输出显示在HTML中
- 向Javascript输出中添加逗号分隔的数字
- 如何在Java程序中读取JavaScript输出文本
- 使用javascript输出样式通知
- Sublime Text 3-can't将javascript输出到浏览器
- c#linq对象到javascript输出
- 如何添加单选按钮和复选框将选择输入到一起以形成JavaScript输出
- JavaScript输出中的CoffeeScript注释
- 以javascript输出对象值
- 你如何格式化json,然后通过Javascript输出它
- 如何将javascript输出到页面
- JavaScript输出消失
- Javascript输出表现得很奇怪