Server-Side XML Validation with CXF Webservice

You can override validation error messages, inserting a line number, by using a custom ValidationEventHandler: package example; import javax.xml.bind.ValidationEvent; import javax.xml.bind.helpers.DefaultValidationEventHandler; public class MyValidationEventHandler extends DefaultValidationEventHandler { @Override public boolean handleEvent(ValidationEvent event) { if (event.getSeverity() == ValidationEvent.WARNING) { return super.handleEvent(event); } else { throw new RuntimeException(event.getMessage() + ” [line:”+event.getLocator().getLineNumber()+”]”); } } } If you configure … Read more

How do I prevent JAXBElement from being generated in a CXF Web Service client?

You have to create a binding file as below, this will get applied globally and use it as wsdl2java – b “bindings.txt” “wsdl” <jaxb:bindings version=”2.1″ xmlns:jaxb=”http://java.sun.com/xml/ns/jaxb” xmlns:xjc=”http://java.sun.com/xml/ns/jaxb/xjc” xmlns:xs=”http://www.w3.org/2001/XMLSchema”> <jaxb:globalBindings generateElementProperty=”false”/> </jaxb:bindings>

How To Modify The Raw XML message of an Outbound CXF Request?

Based on the first comment, I created an abstract class which can easily be used to change the whole soap envelope. Just in case someone wants a ready-to-use code part. import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.IOUtils; import org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor; import org.apache.cxf.io.CachedOutputStream; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.apache.log4j.Logger; /** * http://www.mastertheboss.com/jboss-web-services/apache-cxf-interceptors * http://stackoverflow.com/questions/6915428/how-to-modify-the-raw-xml-message-of-an-outbound-cxf-request … Read more

How to return a partial JSON response using Java?

If you use Jackson (a great JSON lib – kind of the standard for Java I believe), you may use the @View annotation to filter what you want in the resulting object. I understand that you want something dynamic so it’s a bit more complicated. You will find what you are looking for here: http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html … Read more

How to log Apache CXF Soap Request and Soap Response using Log4j?

You need to create a file named org.apache.cxf.Logger (that is: org.apache.cxf file with Logger extension) under /META-INF/cxf/ with the following contents: org.apache.cxf.common.logging.Log4jLogger Reference: Using Log4j Instead of java.util.logging. Also if you replace standard: <cxf:bus> <cxf:features> <cxf:logging/> </cxf:features> </cxf:bus> with much more verbose: <bean id=”abstractLoggingInterceptor” abstract=”true”> <property name=”prettyLogging” value=”true”/> </bean> <bean id=”loggingInInterceptor” class=”org.apache.cxf.interceptor.LoggingInInterceptor” parent=”abstractLoggingInterceptor”/> <bean id=”loggingOutInterceptor” … Read more