Last active
September 1, 2017 03:08
-
-
Save psamsotha/eb30d0e659a2cb6c09dbcd0d712417ee to your computer and use it in GitHub Desktop.
Example default value for multipart field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.media.multipart.*; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import javax.ws.rs.*; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import java.io.InputStream; | |
import java.util.logging.Logger; | |
import static org.junit.Assert.assertEquals; | |
/** | |
* Example with default value for multipart field. | |
* | |
* Dependencies for JUnit test. | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <scope>test</scope> | |
* <version>${jersey2.version}</version> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.glassfish.jersey.media</groupId> | |
* <artifactId>jersey-media-multipart</artifactId> | |
* <version>${jersey2.version}</version> | |
* </dependency> | |
*/ | |
public class MultiPartMissingTest extends JerseyTest { | |
@Path("test") | |
public static class TestResource { | |
@POST | |
@Produces(MediaType.TEXT_PLAIN) | |
@Consumes(MediaType.MULTIPART_FORM_DATA) | |
public String post(@FormDataParam("test") String test, | |
@FormDataParam("file") InputStream file, | |
@FormDataParam("file") FormDataContentDisposition fileFdcd, | |
@FormDataParam("missing") @DefaultValue("default") String missing) { | |
return test + "; " + missing; | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig() | |
.register(TestResource.class) | |
.register(MultiPartFeature.class) | |
.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Override | |
public void configureClient(ClientConfig config) { | |
config.register(MultiPartFeature.class); | |
} | |
@Test | |
public void testIt() { | |
final MultiPart multiPart = new FormDataMultiPart() | |
.field("test", "testing"); | |
final Response response = target("test") | |
.request() | |
.post(Entity.entity(multiPart, multiPart.getMediaType())); | |
assertEquals(200, response.getStatus()); | |
assertEquals("testing; default", response.readEntity(String.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment