我有一些使用Jersey <2.0的经验.现在我正在尝试构建一个war应用程序来提供JSON Webservice API.
我现在正在努力配置Moxy并且接缝比添加的更复杂
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
回到Jersey <2.0的web.xml.
是否有可能只说"请添加json支持"?
目前我只是在服务器上没有任何日志条目的情况下得到很多内部服务器错误错误,只是想"我必须做一些完全错误的事情,这可不是那么难"
任何人都可以给我一个提示吗?
我有这个JSON内容:
{"color":null}
Run Code Online (Sandbox Code Playgroud)
我想用它来制作这些Java对象(反之亦然):
Container
|- List<Entry> entries
|- Entry
|- String key = "color"
|- String value = null
Run Code Online (Sandbox Code Playgroud)
我目前的解决方案总是反序列化"color":null到一个空字符串.我找到了其他解决方案,而不是反序列化null或将String清空null.
我怎样才能获得莫西(或任何其他JAXB实现)反序列化null的null空字符串为空字符串?
我正在使用此代码:
import java.io.ByteArrayOutputStream;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.internal.oxm.ByteArraySource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.oxm.annotations.*;
import org.junit.*;
class Container {
@XmlVariableNode("key")
List<Entry> entries = new ArrayList<Entry>();
}
class Entry {
@XmlTransient
public String key;
@XmlValue
@XmlNullPolicy(nullRepresentationForXml=XmlMarshalNullRepresentation.XSI_NIL, xsiNilRepresentsNull=false)
public String value;
}
public class D {
/** THIS …Run Code Online (Sandbox Code Playgroud) 我试图按照这里的示例,但得到一个javax.xml.bind.PropertyException.由于以下代码行,我收到此异常:
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
Run Code Online (Sandbox Code Playgroud)
我已经完全复制/粘贴了上面列出的示例,因此我的代码正是您在那里看到的.搜索SO和谷歌对此没什么帮助,并且我认为我会把这些带给SO的天才以获得一些帮助.任何帮助都是最受欢迎的,(de)使用JSON和XML与json.org,Jackson和JAXB进行序列化已经变成了一个消耗了近一个月生命的黑色和无底洞.
我的第一印象是我没有正确指定eclipselink运行时(如此处所述),但没有产生解决方案.
堆栈跟踪:
Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(AbstractMarshallerImpl.java:358)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(MarshallerImpl.java:527)
at HelloWorld.main(HelloWorld.java:17)
Run Code Online (Sandbox Code Playgroud)
这就是我在做的事情,

我有REST(Jersey)webservice,它使用了一些编组/解组到XML的数据对象.数据对象位于Web服务战争所依赖的单独项目/ jar中.
我正在使用MOXy作为我的JAXB实现,因为我正在部署到Glassfish并且已经包含了.我知道我需要一个jaxb.properties文件来使用此条目将JAXB实现设置为MOXy:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)
问题是,jaxb.properties文件是否应该包含在数据对象jar或webservice war中,还是两者都包含在内?
我正在尝试发布到需要使用以下代码设置Content-Length标头的Web服务:
// EDIT: added apache connector code
ClientConfig clientConfig = new ClientConfig();
ApacheConnector apache = new ApacheConnector(clientConfig);
// setup client to log requests and responses and their entities
client.register(new LoggingFilter(Logger.getLogger("com.example.app"), true));
Part part = new Part("123");
WebTarget target = client.target("https://api.thing.com/v1.0/thing/{thingId}");
Response jsonResponse = target.resolveTemplate("thingId", "abcdefg")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "anauthcodehere")
.post(Entity.json(part));
Run Code Online (Sandbox Code Playgroud)
从发布说明https://java.net/jira/browse/JERSEY-1617和Jersey 2.0文档https://jersey.java.net/documentation/latest/message-body-workers.html,它暗示Content-长度自动设定.但是,我从服务器返回411响应代码,表明请求中不存在Content-Length.
有谁知道获取Content-Length标头集的最佳方法?
我已通过设置记录器验证了请求中未生成Content-Length标头.
谢谢.
我试图在JavaEE项目中实现Json支持,但是生成了与MOXy相关的异常的问题.我在jersey.java.net上读到,MOXy应该是自动发现的,但是当我尝试时它似乎不起作用.
因此,为了简化这一点,我只是生成了一个新的' jersey-quickstart-webapp '项目并更改了MyResource,如下所示(我的目标是使用Application类而不是web.xml,但这是最简单的方法,可以找到它.无论发生什么错误都会发生.
@Path("myresource")
public class MyResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getIt() {
return Response.status(Response.Status.ACCEPTED).entity(new TestEntity()).build();
}
}
Run Code Online (Sandbox Code Playgroud)
TestEntity类(在同一个包中):
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class TestEntity {
private String content = "SOME CONTENT";
public String getContent() {
return content;
}
}
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a.b.c</groupId>
<artifactId>server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>server</name>
<build>
<finalName>server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> …Run Code Online (Sandbox Code Playgroud) Glassfish4正在使用Moxy将REST响应序列化为JSON.有谁知道如何配置应用程序使用杰克逊而不是Moxy?
我需要在jaxb中将空值显示为空元素.我正在使用jxyb的moxy实现.我找到了这个选项
@XmlNullPolicy(emptyNodeRepresentsNull = true, nullRepresentationForXml = XmlMarshalNullRepresentation.EMPTY_NODE)
Run Code Online (Sandbox Code Playgroud)
是否有类似的扩展可以在类级别应用(对于其中定义的所有元素)
我按照Jersey 2.0文档(https://jersey.java.net/documentation/latest/user-guide.html#json.moxy),修改了pom.xml,包含了jersey-media-moxy工件,编译和安装.我可以为Produces和Consumes案例获得基本的POJO到JSON映射工作.
但是,当我尝试使用复杂数据类型的某些POJO作为资源返回类型时,我得到了很多状态500内部服务器错误但没有任何服务器日志.这很烦人.有人知道这是一个错误还是我错过了一些配置?
顺便说一下,为了对POJO对象使用Moxy映射,POJO需要有一个空参数构造函数.还有其他要求吗?
当我使用此属性封送XML时
marshal.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
它会在最顶部产生一个空的换行符
//Generate empty line break here
<XX>
<YY>
<PDF>pdf name</PDF>
<ZIP>zip name</ZIP>
<RECEIVED_DT>received date time</RECEIVED_DT>
</YY>
</XX>
Run Code Online (Sandbox Code Playgroud)
我认为原因是因为marshal.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);删除<?xml version="1.0" encoding="UTF-8" standalone="yes"?>了输出xml在开头的一个换行符.有没有办法来解决这个问题?我使用JAXB自带JDK 6,Moxy会遇到这个问题吗?
moxy ×10
java ×6
jaxb ×5
jersey ×4
json ×4
eclipselink ×2
jax-rs ×2
glassfish ×1
jackson ×1
java-ee ×1
jersey-2.0 ×1
pojo ×1
properties ×1
rest ×1
web-services ×1
xml ×1