javax.ws.rs.ext.ParamConverter#fromString ( )源码实例Demo

下面列出了javax.ws.rs.ext.ParamConverter#fromString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: cxf   文件: ParamConverterUtils.java
/**
 * Converts the string-based representation of the value to the instance of particular type
 * using parameter converter provider and available parameter converter.
 * @param type type to convert from string-based representation
 * @param provider parameter converter provider to use
 * @param value the string-based representation to convert
 * @return instance of particular type converter from its string representation
 */
@SuppressWarnings("unchecked")
public static< T > T getValue(final Class< T > type, final ParamConverterProvider provider,
        final String value) {

    if (String.class.isAssignableFrom(type)) {
        return (T)value;
    }

    if (provider != null) {
        final ParamConverter< T > converter = provider.getConverter(type, null, new Annotation[0]);
        if (converter != null) {
            return converter.fromString(value);
        }
    }

    throw new IllegalArgumentException(String.format(
            "Unable to convert string '%s' to instance of class '%s': no appropriate converter provided",
            value, type.getName()));
}
 
@Test public void jsonAnnotationReturnsConverterClass() {
  ParamConverter<String> converter =
      provider.getConverter(String.class, String.class, new Annotation[] {
          Annotations.real()
      });
  String value = converter.fromString("\"hey\"");
  assertEquals("hey", value);
  String json = converter.toString("hey");
  assertEquals("\"hey\"", json);
}
 
@Test public void jsonAnnotationReturnsConverterParameterized() {
  Type genericType = Types.newParameterizedType(List.class, String.class);
  ParamConverter<List<String>> converter =
      (ParamConverter) provider.getConverter(List.class, genericType, new Annotation[] {
          Annotations.real()
      });
  List<String> value = converter.fromString("[\"hey\"]");
  assertEquals(singletonList("hey"), value);
  String json = converter.toString(singletonList("hey"));
  assertEquals("[\"hey\"]", json);
}
 
源代码4 项目: portals-pluto   文件: PortletParamProducer.java
@Dependent
@PortletParam
@Produces
public Boolean getBooleanParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Boolean> paramConverter = _getParamConverter(Boolean.class, field.getBaseType(),
			fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Boolean");
	}

	return null;
}
 
源代码5 项目: portals-pluto   文件: PortletParamProducer.java
@Dependent
@PortletParam
@Produces
public Date getDateParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Date> paramConverter = _getParamConverter(Date.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Date");
	}

	return null;
}
 
源代码6 项目: portals-pluto   文件: PortletParamProducer.java
@Dependent
@PortletParam
@Produces
public Double getDoubleParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Double> paramConverter = _getParamConverter(Double.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Double");
	}

	return null;
}
 
源代码7 项目: portals-pluto   文件: PortletParamProducer.java
@Dependent
@PortletParam
@Produces
public Float getFloatParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Float> paramConverter = _getParamConverter(Float.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Float");
	}

	return null;
}
 
源代码8 项目: portals-pluto   文件: PortletParamProducer.java
@Dependent
@PortletParam
@Produces
public Integer getIntegerParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Integer> paramConverter = _getParamConverter(Integer.class, field.getBaseType(),
			fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Integer");
	}

	return null;
}
 
源代码9 项目: portals-pluto   文件: PortletParamProducer.java
@Dependent
@PortletParam
@Produces
public Long getLongParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Long> paramConverter = _getParamConverter(Long.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Long");
	}

	return Long.valueOf(value);
}
 
@Test(expected = IllegalArgumentException.class)
public void invalidLocalDate() {
    ParamConverter<LocalDate> converter =
            provider.getConverter(LocalDate.class, LocalDate.class, null);
    converter.fromString("invalid");
}
 
 同类方法