|
Apache CXF example source code file (DataBindingJSONProviderTest.java)
The Apache CXF DataBindingJSONProviderTest.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.cxf.jaxrs.provider; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; import org.apache.cxf.aegis.databinding.AegisDatabinding; import org.apache.cxf.databinding.DataBinding; import org.apache.cxf.jaxb.JAXBDataBinding; import org.apache.cxf.jaxrs.JAXRSServiceImpl; import org.apache.cxf.jaxrs.impl.MetadataMap; import org.apache.cxf.jaxrs.model.ClassResourceInfo; import org.apache.cxf.jaxrs.resources.Book; import org.apache.cxf.jaxrs.resources.sdo.Structure; import org.apache.cxf.jaxrs.resources.sdo.impl.StructureImpl; import org.apache.cxf.jaxrs.utils.ResourceUtils; import org.apache.cxf.sdo.SDODataBinding; import org.apache.cxf.service.Service; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; public class DataBindingJSONProviderTest extends Assert { private ClassResourceInfo c; private ClassResourceInfo c2; @Before public void setUp() { c = ResourceUtils.createClassResourceInfo(TheBooks.class, TheBooks.class, true, true); c2 = ResourceUtils.createClassResourceInfo(TheSDOBooks.class, TheSDOBooks.class, true, true); } @SuppressWarnings("unchecked") @Test public void testSDOWrite() throws Exception { Service s = new JAXRSServiceImpl(Collections.singletonList(c2), true); DataBinding binding = new SDODataBinding(); binding.initialize(s); DataBindingJSONProvider p = new DataBindingJSONProvider(); p.setDataBinding(binding); p.setNamespaceMap(Collections.singletonMap("http://apache.org/structure/types", "p0")); Structure struct = new StructureImpl(); struct.getTexts().add("text1"); struct.setText("sdo"); struct.setInt(3); struct.setDbl(123.5); ByteArrayOutputStream bos = new ByteArrayOutputStream(); p.writeTo(struct, Structure.class, Structure.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos); String data = "{\"p0.Structure\":{\"@xsi.type\":\"p0:Structure\",\"p0.text\":\"sdo\",\"p0.int\":3" + ",\"p0.dbl\":123.5,\"p0.texts\":\"text1\"}}"; assertEquals(bos.toString(), data); } @SuppressWarnings("unchecked") @Test public void testSDORead() throws Exception { String data = "{\"p0.Structure\":{\"@xsi.type\":\"p0:Structure\",\"p0.text\":\"sdo\",\"p0.int\":3" + ",\"p0.dbl\":123.5,\"p0.texts\":\"text1\"}}"; Service s = new JAXRSServiceImpl(Collections.singletonList(c2), true); DataBinding binding = new SDODataBinding(); binding.initialize(s); DataBindingJSONProvider p = new DataBindingJSONProvider(); p.setDataBinding(binding); p.setNamespaceMap(Collections.singletonMap("http://apache.org/structure/types", "p0")); ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes()); Structure struct = (Structure)p.readFrom((Class)Structure.class, Structure.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), is); assertEquals("sdo", struct.getText()); assertEquals(123.5, struct.getDbl(), 0.01); assertEquals(3, struct.getInt()); } @Test public void testJAXBWrite() throws Exception { Service s = new JAXRSServiceImpl(Collections.singletonList(c), true); DataBinding binding = new JAXBDataBinding(); binding.initialize(s); DataBindingJSONProvider<Book> p = new DataBindingJSONProvider Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF DataBindingJSONProviderTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.