Skip to content

Commit

Permalink
RESTEASY-1841
Browse files Browse the repository at this point in the history
Create code example to show how to inject JSON format data into @MultipartForm parameter correctly
  • Loading branch information
liweinan authored and asoldano committed Apr 9, 2018
1 parent 98afb64 commit c3c9eb6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
@@ -0,0 +1,51 @@
package org.jboss.resteasy.test.providers.jackson2.multipart;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
@RunAsClient
public class Jackson2MultipartFormTest {

static ResteasyClient client;

@BeforeClass
public static void init() {
client = new ResteasyClientBuilder().build();
}

@AfterClass
public static void close() {
client.close();
}

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(Jackson2MultipartFormTest.class.getSimpleName());
war.addClass(Jackson2MultipartFormTest.class);
return TestUtil.finishContainerPrepare(war, null, JsonFormResource.class, JsonUser.class);
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, Jackson2MultipartFormTest.class.getSimpleName());
}

@Test
public void testJacksonProxy() {
JsonForm proxy = client.target(generateURL("")).proxy(JsonForm.class);
String name = proxy.putMultipartForm(new JsonFormResource.Form(new JsonUser("bill")));
Assert.assertEquals("bill", name);
}
}
@@ -0,0 +1,16 @@
package org.jboss.resteasy.test.providers.jackson2.multipart;

import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

@Path("/")
public interface JsonForm {

@PUT
@Path("form/class")
@Consumes("multipart/form-data")
String putMultipartForm(@MultipartForm JsonFormResource.Form form);
}
@@ -0,0 +1,40 @@
package org.jboss.resteasy.test.providers.jackson2.multipart;

import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.jboss.resteasy.annotations.providers.multipart.PartType;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

@Path("/")
public class JsonFormResource {

public JsonFormResource() {
}

public static class Form {
@FormParam("user")
@PartType("application/json")
private JsonUser user;

public Form() {
}

public Form(final JsonUser user) {
this.user = user;
}

public JsonUser getUser() {
return user;
}
}

@PUT
@Path("form/class")
@Consumes("multipart/form-data")
public String putMultipartForm(@MultipartForm Form form) {
return form.getUser().getName();
}
}
@@ -0,0 +1,20 @@
package org.jboss.resteasy.test.providers.jackson2.multipart;

public class JsonUser {
private String name;

public JsonUser() {
}

public JsonUser(final String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

0 comments on commit c3c9eb6

Please sign in to comment.