Test out jackson for JSON mapping

This commit is contained in:
Stuart Douglas
2018-07-20 12:32:20 +10:00
parent b1c8b2bac0
commit bfd72bfb8c
7 changed files with 116 additions and 11 deletions

View File

@@ -46,6 +46,7 @@ import org.jboss.shamrock.runtime.StartupContext;
import org.jboss.shamrock.runtime.StartupTask;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
@@ -292,6 +293,17 @@ public class Runner {
for (String holder : reflectiveClasses) {
Label lTryBlockStart = new Label();
Label lTryBlockEnd = new Label();
Label lCatchBlockStart = new Label();
Label lCatchBlockEnd = new Label();
// set up try-catch block for RuntimeException
mv.visitTryCatchBlock(lTryBlockStart, lTryBlockEnd,
lCatchBlockStart, "java/lang/Exception");
// started the try block
mv.visitLabel(lTryBlockStart);
mv.visitLdcInsn(1);
mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
mv.visitInsn(DUP);
@@ -311,6 +323,9 @@ public class Runner {
//now load everything else
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getMethods", "()[Ljava/lang/reflect/Method;", false);
mv.visitMethodInsn(INVOKESTATIC, "org/graalvm/nativeimage/RuntimeReflection", "register", "([Ljava/lang/reflect/Executable;)V", false);
mv.visitLabel(lTryBlockEnd);
mv.visitLabel(lCatchBlockStart);
mv.visitLabel(lCatchBlockEnd);
}
mv.visitInsn(RETURN);
mv.visitMaxs(0, 2);

View File

@@ -40,6 +40,10 @@
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-json-p-provider</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>

View File

@@ -15,17 +15,18 @@ public class TestResource {
}
@GET
@Path("/json")
@Path("/jackson")
@Produces("application/json")
public MyData get() {
MyData m = new MyData();
m.setName("Stuart Douglas");
m.setValue("A value");
m.setName("Stuart");
m.setValue("A Value");
return m;
}
@GET
@Path("/jsonp")
@Produces("application/json")
public JsonObject jsonp() {
return Json.createObjectBuilder()
.add("name", "Stuart")

View File

@@ -0,0 +1,68 @@
package org.jboss.shamrock.example.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.json.Json;
import javax.json.JsonObject;
import org.jboss.shamrock.junit.GraalTest;
import org.jboss.shamrock.junit.ShamrockTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(GraalTest.class)
public class JaxRSITCase {
@Test
public void testJAXRS() throws Exception {
URL uri = new URL("http://localhost:8080/rest/test");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
Assert.assertEquals("TEST", new String(out.toByteArray()));
}
@Test
public void testJsonp() throws Exception {
URL uri = new URL("http://localhost:8080/rest/test/jsonp");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
JsonObject obj = Json.createReader(new ByteArrayInputStream(out.toByteArray())).readObject();
Assert.assertEquals("Stuart", obj.getString("name"));
Assert.assertEquals("A Value", obj.getString("value"));
}
@Test
public void testJackson() throws Exception {
URL uri = new URL("http://localhost:8080/rest/test/jackson");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
JsonObject obj = Json.createReader(new ByteArrayInputStream(out.toByteArray())).readObject();
Assert.assertEquals("Stuart", obj.getString("name"));
Assert.assertEquals("A Value", obj.getString("value"));
}
}

View File

@@ -48,4 +48,20 @@ public class JaxRSTestCase {
Assert.assertEquals("A Value", obj.getString("value"));
}
@Test
public void testJackson() throws Exception {
URL uri = new URL("http://localhost:8080/rest/test/jackson");
URLConnection connection = uri.openConnection();
InputStream in = connection.getInputStream();
byte[] buf = new byte[100];
int r;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((r = in.read(buf)) > 0) {
out.write(buf, 0, r);
}
JsonObject obj = Json.createReader(new ByteArrayInputStream(out.toByteArray())).readObject();
Assert.assertEquals("Stuart", obj.getString("name"));
Assert.assertEquals("A Value", obj.getString("value"));
}
}

View File

@@ -134,14 +134,10 @@ public class JaxrsScanningProcessor implements ResourceProcessor {
@Override
public void process(ArchiveContext archiveContext, ProcessorContext processorContext) throws Exception {
try {
//this is pretty yuck, and does not really belong here, but it is needed to get the json-p
//provider to work
Class.forName("org.glassfish.json.JsonProviderImpl");
processorContext.addReflectiveClass("org.glassfish.json.JsonProviderImpl");
} catch (ClassNotFoundException e) {
}
//this is pretty yuck, and does not really belong here, but it is needed to get the json-p
//provider to work
processorContext.addReflectiveClass("org.glassfish.json.JsonProviderImpl");
processorContext.addReflectiveClass("com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector");
Index index = archiveContext.getIndex();
List<AnnotationInstance> app = index.getAnnotations(APPLICATION_PATH);
if (app.isEmpty()) {

View File

@@ -141,6 +141,11 @@
<artifactId>resteasy-json-p-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>