本文整理汇总了Java中org.eclipse.persistence.jaxb.UnmarshallerProperties类的典型用法代码示例。如果您正苦于以下问题:Java UnmarshallerProperties类的具体用法?Java UnmarshallerProperties怎么用?Java UnmarshallerProperties使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UnmarshallerProperties类属于org.eclipse.persistence.jaxb包,在下文中一共展示了UnmarshallerProperties类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFrom
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
public AccessToken readFrom(Class<AccessToken> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext jc = JAXBContext.newInstance(AccessToken.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in
// the
// JSON input
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
StreamSource streamSource = new StreamSource(entityStream);
AccessToken accessToken = unmarshaller.unmarshal(streamSource, AccessToken.class).getValue();
return accessToken;
} catch (JAXBException e) {
LOG.error("can't unmarshall response",e);
throw new RuntimeException("can't unmarshall response");
}
}
示例2: testUnmarschallingMessage
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
@Test
public void testUnmarschallingMessage() throws Exception{
JAXBContext jc = JAXBContext.newInstance(Message.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in
// the
// JSON input
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
String json = "{\"message\":\"success\"}";
StreamSource stream =
new StreamSource(new StringReader(json));
Message message = unmarshaller.unmarshal(stream, Message.class).getValue();
Assert.assertNotNull(message);
Assert.assertTrue(message.getMessage().equals("success"));
}
示例3: testUnmarschallingAccessToken
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
/**
* test unmarschalling of AccessToken
*
* @throws Exception
*/
@Test
public void testUnmarschallingAccessToken() throws Exception {
JAXBContext jc = JAXBContext.newInstance(AccessToken.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in
// the
// JSON input
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
String json = "{\"access_token\":\"eyJhbGciOiJSU\""
+ " ,\"expires_in\":60,\"refresh_expires_in\":600," +
"\"refresh_token\":\"eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lk\"," +
"\"not-before-policy\":0,\"session_state\":\"a1f9f252-ff0f-4a45-bb9c-bec019eab79a\"}";
LOG.info("ummarshall token:" + json);
StreamSource stream =
new StreamSource(new StringReader(json));
AccessToken accessToken = unmarshaller.unmarshal(stream, AccessToken.class).getValue();
Assert.assertNotNull(accessToken);
}
示例4: deserialize
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
@Override
public Object deserialize(InputSource inputSource, Class<?>... types) throws SerializationException {
try {
JAXBContext context = createJAXBContextJson(types);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
unmarshaller.setProperty(UnmarshallerProperties.JSON_TYPE_COMPATIBILITY, true);
Object object = unmarshaller.unmarshal(inputSource);
if (object instanceof JAXBElement) {
// 因为不包括头,此处返回的是这个玩意儿
return ((JAXBElement<?>) object).getValue();
} else {
return object;
}
} catch (JAXBException e) {
throw new SerializationException(e);
}
}
示例5: transformJsonResponse
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
private Suggestions transformJsonResponse(final InputStream is, final Path transformation) throws IOException, TransformationException {
try(final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
jsonTransformer.transform(is, transformation, os);
try(final InputStream resultIs = new ByteArrayInputStream(os.toByteArray())) {
final JAXBContext context = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[]{Suggestions.class}, null);
final Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, org.eclipse.persistence.oxm.MediaType.APPLICATION_JSON);
unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, null);
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR, ':');
final JAXBElement<Suggestions> jaxbElement = unmarshaller.unmarshal(new StreamSource(resultIs), Suggestions.class);
return jaxbElement.getValue();
} catch(final JAXBException e) {
throw new TransformationException(e);
}
}
}
示例6: stringToObj
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
/**
* Converts a resource XML representation data into resource Java object.
*
* @param representation
* - resource XML representation
* @return resource Java object
*/
@Override
public Object stringToObj(String representation) {
if(representation.isEmpty()){
return null;
}
StringReader stringReader = new StringReader(representation);
try {
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, mediaType);
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
return unmarshaller.unmarshal(stringReader);
} catch (JAXBException e) {
LOGGER.error("JAXB unmarshalling error!", e);
}
return null;
}
示例7: initialize
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
private void initialize() throws JAXBException {
um = context.createUnmarshaller();
// Set the Unmarshaller media type to JSON or XML
um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in the
//um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, IsJsonIncludeRoot);
m = context.createMarshaller();
//m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in the JSON output
//m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
// Set it to true if you need the JSON output to formatted
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}
示例8: initialize
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
private void initialize() throws JAXBException {
um = context.createUnmarshaller();
// Set the Unmarshaller media type to JSON or XML
um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in the
// um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, isJsonIncludeRoot);
m = context.createMarshaller();
//m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in the JSON output
//m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
// Set it to true if you need the JSON output to formatted
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}
示例9: initialize
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
private void initialize(String schema) throws JAXBException {
um = context.createUnmarshaller();
um.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/xml");
m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE)) {
return "xsi";
} else
return "m2m";
}
});
String schemaLocation = schema;
if(schemaLocation != null) {
m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.onem2m.org/xml/protocols " + schemaLocation);
}
m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
}
示例10: jaxbUnmarshall
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object jaxbUnmarshall(Object schemaObject, String json) throws JAXBException {
Class cls = schemaObject.getClass();
Class[] types = new Class[1];
types[0] = cls;
Map<String, String> namespacePrefixMapper = new HashMap<>(3);
namespacePrefixMapper.put("router", "router");
namespacePrefixMapper.put("provider", "provider");
namespacePrefixMapper.put("binding", "binding");
Map<String, Object> jaxbProperties = new HashMap<>(2);
jaxbProperties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
jaxbProperties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
jaxbProperties.put(JAXBContextProperties.JSON_NAMESPACE_SEPARATOR, ':');
jaxbProperties.put(JAXBContextProperties.NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
JAXBContext jc = JAXBContext.newInstance(types, jaxbProperties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
StringReader reader = new StringReader(json);
StreamSource stream = new StreamSource(reader);
return unmarshaller.unmarshal(stream, cls).getValue();
}
示例11: newMarshalContainer
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
private static MarshalContainer newMarshalContainer(ClassLoader cl)throws JaxbSimpleException{
try{
JAXBContext jc = JAXBContext.newInstance(ModelNS.list, cl);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MediaType mediaType = MediaType.APPLICATION_JSON;
unmarshaller.setProperty("eclipselink.media-type", mediaType.getMediaType());
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty("eclipselink.media-type", mediaType.getMediaType());
marshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
return new MarshalContainer(unmarshaller, marshaller);
}catch(JAXBException e){
throw new JaxbSimpleException(e);
}
}
示例12: readFrom
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
public Message readFrom(Class<Message> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext jc = JAXBContext.newInstance(Message.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
// unmarshaller.setProperty("javax.xml.bind.context.factory","org.eclipse.persistence.jaxb.JAXBContextFactory");
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
// Set it to true if you need to include the JSON root element in
// the
// JSON input
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, "@");
StreamSource streamSource = new StreamSource(entityStream);
Message message = unmarshaller.unmarshal(streamSource, Message.class).getValue();
return message;
} catch (JAXBException e) {
LOG.error("can't unmarshall response",e);
throw new RuntimeException("can't unmarshall response");
}
}
示例13: deserializeInternal
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
private <T> T deserializeInternal(StreamSource streamSource, Class<T> clazz) throws SerializerException {
try {
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
return unmarshaller.unmarshal(streamSource, clazz).getValue();
} catch (JAXBException e) {
log.error("Can't deserialize object of type {}", clazz.getSimpleName());
throw new SerializerException("Can't deserialize object of type " + clazz.getSimpleName(), e);
}
}
示例14: deserialize
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
@Override
public Object deserialize(InputSource inputSource, Class<?>... types) throws SerializationException {
try {
JAXBContext context = createJAXBContextJson(types);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
return unmarshaller.unmarshal(inputSource);
} catch (JAXBException e) {
throw new SerializationException(e);
}
}
示例15: getObject
import org.eclipse.persistence.jaxb.UnmarshallerProperties; //导入依赖的package包/类
public static <T> T getObject(String s, Class<T> intendedClass) {
try {
JAXBContext c = JAXBContextFactory.createContext(new Class[]{intendedClass}, null);
Unmarshaller um = c.createUnmarshaller();
um.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
um.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
StreamSource ss = new StreamSource(new StringReader(s));
return um.unmarshal(ss, intendedClass).getValue();
}
catch (JAXBException e) {
throw new UserInputException("Get Object from JSON String failed.", e);
}
}