MultipartBody { public <T> T getValueAsInstance(String key, Class<T> clazz) throws IOException { String valueAsString = getValueAsString(key); if (valueAsString == null) { throw new BadRequestException( "Missing JSON property with the key: " + key); } return _parseValue(valueAsString, clazz); } private  MultipartBody(
		Map<String, BinaryFile> binaryFiles,
		ObjectMapperProvider objectMapperProvider, Map<String, String> values); static MultipartBody of(
		Map<String, BinaryFile> binaryFiles,
		ObjectMapperProvider objectMapperProvider, Map<String, String> values); BinaryFile getBinaryFile(String key); byte[] getBinaryFileAsBytes(String key); T getValueAsInstance(String key, Class<T> clazz); Optional<T> getValueAsInstanceOptional(
			String key, Class<T> clazz); String getValueAsString(String key); Map<String, String> getValues();  }
@Test public void testGetValueAsInstance() throws IOException { MultipartBody multipartBody = MultipartBody.of( Collections.emptyMap(), __ -> _objectMapper, Collections.singletonMap( "key", JSONUtil.put( "list", Arrays.asList(1, 2, 3) ).put( "number", 42 ).put( "string", "Hello" ).toString())); TestClass testClass = multipartBody.getValueAsInstance( "key", TestClass.class); assertThat(testClass.list, contains(1, 2, 3)); assertThat(testClass.number, is(42L)); assertThat(testClass.string, is("Hello")); assertThat(testClass.testClass, is(nullValue())); try { multipartBody.getValueAsInstance("null", TestClass.class); throw new AssertionError("Should thrown exception"); } catch (Exception exception) { assertThat(exception, is(instanceOf(BadRequestException.class))); assertThat( exception.getMessage(), is("Missing JSON property with the key: null")); } multipartBody = MultipartBody.of( Collections.emptyMap(), __ -> null, Collections.singletonMap("key", "value")); try { multipartBody.getValueAsInstance("key", TestClass.class); throw new AssertionError(); } catch (Exception exception) { assertThat( exception, is(instanceOf(InternalServerErrorException.class))); String expectedMessage = "Unable to get object mapper for class " + TestClass.class.getName(); assertThat(exception.getMessage(), is(expectedMessage)); } }