当前位置: 首页 > news >正文

有哪些建设网站公司进入电商平台832采购

有哪些建设网站公司,进入电商平台832采购,帝国网站数据库配置文件,手表网站排名前十jax-rs jax-wsBean验证简介 JavaBeans验证#xff08;Bean验证#xff09;是Java EE 6平台的一部分提供的新验证模型。 约束通过以JavaBeans组件#xff08;例如托管Bean#xff09;的字段#xff0c;方法或类上的注释形式的约束来支持Bean验证模型。 javax.validation.c… jax-rs jax-ws Bean验证简介 JavaBeans验证Bean验证是Java EE 6平台的一部分提供的新验证模型。 约束通过以JavaBeans组件例如托管Bean的字段方法或类上的注释形式的约束来支持Bean验证模型。 javax.validation.constraints包中提供了几个内置约束。 Java EE 6教程列出了所有内置约束。 Bean验证中的约束通过Java注释表示 public class Person {NotNullSize(min 2, max 50)private String name;// ... }Bean验证和RESTful Web服务 JAX-RS 1.0为提取请求值并将其绑定到Java字段属性和参数使用HeaderParam QueryParam等注释提供了强大的支持。它还支持通过非注释参数将请求实体主体绑定到Java对象中也就是说未使用任何JAX-RS注释进行注释的参数。 当前必须以编程方式对资源类中的这些值进行任何其他验证。 下一个发行版JAX-RS 2.0包含一项建议以使验证批注可以与JAX-RS批注结合使用。 例如给定验证批注Pattern 以下示例显示如何验证表单参数。 GET Path({id}) public Person getPerson(PathParam(id)Pattern(regexp [0-9], message The id must be a valid number)String id) {return persons.get(id); } 但是目前唯一的解决方案是使用专有实现。 接下来介绍的是基于JBoss的RESTEasy框架的解决方案该解决方案符合JAX-RS规范并通过注释ValidateRequest添加了RESTful验证接口。 导出的接口允许我们创建自己的实现。 但是已经有一种广泛使用的方法RESTEasy还向其提供了无缝集成。 这个实现是Hibernate Validator 。 可以通过以下Maven依赖项将此提供程序添加到项目中 dependencygroupIdorg.jboss.resteasy/groupIdartifactIdresteasy-jaxrs/artifactIdversion2.3.2.Final/versionscopeprovided/scope /dependency dependencygroupIdorg.jboss.resteasy/groupIdartifactIdresteasy-hibernatevalidator-provider/artifactIdversion2.3.2.Final/version /dependency 注意 在类或方法级别不声明ValidateRequest情况下 ValidateRequest在方法上应用了约束注释也不会进行验证例如上面的示例。 GET Path({id}) ValidateRequest public Person getPerson(PathParam(id)Pattern(regexp [0-9], message The id must be a valid number)String id) {return persons.get(id); } 应用注释后发出请求时将自动验证参数id 。 您当然可以通过使用注释Valid验证整个实体而不是单个字段。 例如我们可以有一个接受Person对象并对其进行验证的方法。 POST Path(/validate) ValidateRequest public Response validate(Valid Person person) {// ... } 注意 默认情况下当验证失败时容器将引发异常并将HTTP 500状态返回给客户端。 可以/应该重写此默认行为使我们能够自定义通过异常映射器返回给客户端的Response 。 国际化 到目前为止我们一直在使用默认的或硬编码的错误消息但这既是一种不好的做法又一点也不灵活。 I18N是Bean验证规范的一部分它使我们能够使用资源属性文件来指定自定义错误消息。 默认资源文件名称为ValidationMessages.properties并且必须包含属性/值对例如 person.id.patternThe person id must be a valid number person.name.sizeThe person name must be between {min} and {max} chars long 注意 {min}和{max}是指与消息关联的约束的属性。 然后可以将这些已定义的消息注入验证约束中如下所示 POST Path(create) Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response createPerson(FormParam(id)Pattern(regexp [0-9], message {person.id.pattern})String id,FormParam(name)Size(min 2, max 50, message {person.name.size})String name) {Person person new Person();person.setId(Integer.valueOf(id));person.setName(name);persons.put(Integer.valueOf(id), person);return Response.status(Response.Status.CREATED).entity(person).build(); } 要提供其他语言的翻译必须使用翻译后的消息创建一个新的ValidationMessages_XX.properties文件其中XX是所提供语言的代码。 不幸的是Hibernate Validator提供程序不基于特定的HTTP请求支持I18N。 它不考虑Accept-Language HTTP标头并且始终使用Locale.getDefault()提供的默认Locale 。 为了能够使用Accept-Language HTTP标头更改Locale 必须提供一个自定义实现。 定制验证器提供者 以下代码旨在解决此问题并已通过JBoss AS 7.1进行了测试。 首先要做的是删除Maven resteasy-hibernatevalidator-provider依赖性因为我们提供了自己的提供程序并添加了Hibernate Validator依赖性 dependencygroupIdorg.hibernate/groupIdartifactIdhibernate-validator/artifactIdversion4.2.0.Final/version /dependency 接下来创建一个自定义消息插值器以调整使用的默认Locale 。 public class LocaleAwareMessageInterpolator extendsResourceBundleMessageInterpolator {private Locale defaultLocale Locale.getDefault();public void setDefaultLocale(Locale defaultLocale) {this.defaultLocale defaultLocale;}Overridepublic String interpolate(final String messageTemplate,final Context context) {return interpolate(messageTemplate, context, defaultLocale);}Overridepublic String interpolate(final String messageTemplate,final Context context, final Locale locale) {return super.interpolate(messageTemplate, context, locale);} } 下一步是提供ValidatorAdapter 。 引入此接口是为了将RESTEasy与实际的验证API分离。 public class RESTValidatorAdapter implements ValidatorAdapter {private final Validator validator;private final MethodValidator methodValidator;private final LocaleAwareMessageInterpolator interpolator new LocaleAwareMessageInterpolator();public RESTValidatorAdapter() {Configuration? configuration Validation.byDefaultProvider().configure();this.validator configuration.messageInterpolator(interpolator).buildValidatorFactory().getValidator();this.methodValidator validator.unwrap(MethodValidator.class);}Overridepublic void applyValidation(Object resource, Method invokedMethod,Object[] args) {// For the i8n to work, the first parameter of the method being validated must be a HttpHeadersif ((args ! null) (args[0] instanceof HttpHeaders)) {HttpHeaders headers (HttpHeaders) args[0];ListLocale acceptedLanguages headers.getAcceptableLanguages();if ((acceptedLanguages ! null) (!acceptedLanguages.isEmpty())) {interpolator.setDefaultLocale(acceptedLanguages.get(0));}}ValidateRequest resourceValidateRequest FindAnnotation.findAnnotation(invokedMethod.getDeclaringClass().getAnnotations(), ValidateRequest.class);if (resourceValidateRequest ! null) {SetConstraintViolation? constraintViolations new HashSetConstraintViolation?(validator.validate(resource,resourceValidateRequest.groups()));if (constraintViolations.size() 0) {throw new ConstraintViolationException(constraintViolations);}}ValidateRequest methodValidateRequest FindAnnotation.findAnnotation(invokedMethod.getAnnotations(), ValidateRequest.class);DoNotValidateRequest doNotValidateRequest FindAnnotation.findAnnotation(invokedMethod.getAnnotations(),DoNotValidateRequest.class);if ((resourceValidateRequest ! null || methodValidateRequest ! null) doNotValidateRequest null) {SetClass? set new HashSetClass?();if (resourceValidateRequest ! null) {for (Class? group : resourceValidateRequest.groups()) {set.add(group);}}if (methodValidateRequest ! null) {for (Class? group : methodValidateRequest.groups()) {set.add(group);}}SetMethodConstraintViolation? constraintViolations new HashSetMethodConstraintViolation?(methodValidator.validateAllParameters(resource,invokedMethod, args,set.toArray(new Class?[set.size()])));if (constraintViolations.size() 0) {throw new MethodConstraintViolationException(constraintViolations);}}} } 警告 HttpHeaders需要作为要验证的方法的第一个参数注入 POST Path(create) Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response createPerson(Context HttpHeaders headers,FormParam(id)Pattern(regexp [0-9], message {person.id.pattern})String id,FormParam(name)Size(min 2, max 50, message {person.name.size})String name) {Person person new Person();person.setId(Integer.valueOf(id));person.setName(name);persons.put(id, person);return Response.status(Response.Status.CREATED).entity(person).build(); } 最后创建将选择以上用于验证Bean验证约束的类的提供程序 Provider public class RESTValidatorContextResolver implementsContextResolverValidatorAdapter {private static final RESTValidatorAdapter adapter new RESTValidatorAdapter();Overridepublic ValidatorAdapter getContext(Class? type) {return adapter;} }映射异常 Bean验证API使用类型为javax.validation.ValidationException或其任何子类的异常报告错误情况。 应用程序可以为任何异常提供自定义异常映射提供程序。 JAX-RS实现必须始终使用其泛型类型是异常的最接近超类的提供程序其中应用程序定义的提供程序优先于内置提供程序。 异常映射器可能看起来像 Provider public class ValidationExceptionMapper implementsExceptionMapperMethodConstraintViolationException {Overridepublic Response toResponse(MethodConstraintViolationException ex) {MapString, String errors new HashMapString, String();for (MethodConstraintViolation? methodConstraintViolation : ex.getConstraintViolations()) {errors.put(methodConstraintViolation.getParameterName(),methodConstraintViolation.getMessage());}return Response.status(Status.PRECONDITION_FAILED).entity(errors).build();} } 上面的示例显示了ExceptionMapper的实现该映射映射了MethodConstraintViolationException类型的MethodConstraintViolationException 。 当用ValidateRequest注释的方法的一个或多个参数的验证失败时Hibernate Validator实现将引发此异常。 这样可以确保客户端收到格式化的响应而不仅仅是从资源传播的异常。 源代码 这篇文章使用的源代码可以在GitHub上找到 。 警告确保更改资源属性文件名以使文件ValidationMessages.properties 即没有任何后缀映射到Locale.getDefault()返回的Locale 。 作者Samuel Santos是Java和开放源代码传播者也是PT.JUG葡萄牙Java用户组的JUG负责人。 他是葡萄牙科英布拉市Present Technologies的技术主管负责刺激创新知识共享教练和技术选择活动。 塞缪尔Samuel是博客samaxes.com的作者并以samaxes鸣叫。 参考 Java Advent Calendar博客上我们JCG合作伙伴 Samuel Santos的JAX-RS Bean验证错误消息国际化 。 翻译自: https://www.javacodegeeks.com/2012/12/jax-rs-bean-validation-error-message-internationalization.htmljax-rs jax-ws
http://www.zqtcl.cn/news/992485/

相关文章:

  • 好看响应式网站模板下载可以访问的国外网站
  • 做电脑网站宽度网站建立安全连接失败
  • 西安网站设计哪家公司好my12777域名查询
  • 西宁网站建设排名网站设计对网站建设有哪些意义?
  • 北京平台网站建设价位怎样做网站卖网站
  • 网站建设与维护试题a卷建设银行官方网站买五粮液酒
  • 安装网站源码做文艺文创产品的网站
  • 软件公司网站设计与制作电子商务成功网站的案例
  • 购物车功能网站怎么做的建设众筹类网站
  • 哪些网站做的美爱站工具网
  • 对网站开发的理解源码资源网
  • 有哪些做兼职的网站网站建设的项目计划书
  • 如何做行业网站江苏城乡建设部网站首页
  • 淘客网站怎么做 知乎wordpress 删除插件
  • 深圳市住房和建设局人事调整公司网站seo怎么做
  • 小型影视网站源码好看的个人博客
  • 哈密建设厅网站西安培训机构
  • 网站建设公众号小程序开发密云网站开发
  • 南宁网站开发制作网站数据库分离怎么做
  • 不能打开建设银行网站怎么办自己做网站要会什么软件下载
  • 微软网站怎么做的diy个性定制
  • 洛阳做网站的公司宣传品牌网站建设
  • 公司网站开发招标书电子商务网站系统
  • 哪个旅游网站做的最好营销策划方案范文免费下载
  • 德州哪家网站优化公司专业单页网站seo如何优化
  • 南昌汉邦网站建设城建局
  • 网站建设:中企动力招聘网58同城招聘发布
  • 惠州住房和建设局网站物流网站建设方案范文
  • 做网站架构需要什么步骤wordpress插件连不上
  • 网上购物网站建设规划论文国家企业网官网查询