Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:37:30+00:00 2026-06-03T02:37:30+00:00

We are using JAX-RS with some fairly basic POJO entities and have a number

  • 0

We are using JAX-RS with some fairly basic POJO entities and have a number of @GET and @POST annotated methods that @Produce and @Consume MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML. Nothing spectacular.

Question I have is how best should I validate the data coming in?

We do not have an XML Schema although I could generate one. I would need to hook this in somehow which doesn’t look terribly appealing and I have not found a concise example yet.

We could use “bean validation” but I am again unsure how I might hook this up and invoke it.

Finally (I think) we could for example add some isValidForXXX() methods to the entity POJOs and call them whenever we have an instance handed in to us.

Recommendations anyone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:37:32+00:00Added an answer on June 3, 2026 at 2:37 am

    If you have an XML schema then you could use JAXB validation inside a MessageBodyReader. For a concrete example see my answer to a similar question.

    • Validate JAXBElement in JPA/JAX-RS Web Service

    ValidatingReader

    Below is a bare bones implementation of MessageBodyReader that does four things: 1) Create a JAXBContext, 2) Create an instance of Schema, 3) Sets the schema on the Unmarshaller 4) Unmarshals the InputStream.

    package org.example;
    
    import java.io.*;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Type;
    import java.net.URL;
    
    import javax.ws.rs.*;
    import javax.ws.rs.core.*;
    import javax.ws.rs.ext.*;
    import javax.xml.XMLConstants;
    import javax.xml.bind.*;
    import javax.xml.validation.*;
    
    @Provider
    @Consumes("application/xml")
    public class ValidatingReader implements MessageBodyReader<Customer> {
    
        @Context
        protected Providers providers;
    
        private Schema schema;
        private JAXBContext jaxbContext;
    
        public ValidatingReader() {
            try {
                JAXBContext jc = JAXBContext.newInstance(Customer.class);
                SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                URL schemaURL = null; // URL for your XML schema
                schema = sf.newSchema(schemaURL);
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
            return arg0 == Customer.class;
        }
    
        public Customer readFrom(Class<Customer> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5)
                throws IOException, WebApplicationException {
            try {
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                unmarshaller.setSchema(schema);
                return (Customer) unmarshaller.unmarshal(arg5);
            } catch(JAXBException e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have built some REST (jax-rs) web services using Apache CXF. They return a
I Developed a REST Service in Jax-RS, and I have some functions that use
I'm trying to consume some .NET web services using JAX-WS. I have generated the
I have a Spring application that publishes some RESTful web services using Apache CXF
I have a webservice that I'm using JAX-WS annotations to generate the WSDL &
I am using JAX-WS and I am having trouble retrieving the client information that
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,
I have a problem when generating java classes from WSDL using JAX-RPC wscompile ANT
I am using JAX-WS to export some SOAP Web Service Endpoints. I know I
I have a JAX-RS REST service implemented using Jersey. One of the cool features

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.