How to remove namespace from Jaxb2 generated xml - java

I have a problem a little problem related to the namespaces in the XML marshalled by Jaxb2 . When I print out the xml file, I get this "extra" annotations on the xml tags. Is there any way to get rid of the extra namespace annotations ?
Here is an example:
<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns:ns2="http://www.something.com/something">
<ns2:food>steak</ns2:food>
<ns2:beverage>water</ns2:beverage>
</root>
I want to get rid of the ns2: namespace.
I tried with :
<bean id="glsJaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="${gls.jaxb2.contextpath}"/>
<property name="marshallerProperties">
<map>
<entry key="com.sun.xml.bind.namespacePrefixMapper">
<bean class="es.fs.fswarehousing.gls.ws.EmptyNamespacePrefixMapper"/>
</entry>
</map>
</property>
</bean>
But im getting :
ERROR [TaskExecutor-master-555-ProcessTask [8796749431734]]
[ActionNode] Error executing
org.springframework.oxm.UncategorizedMappingException: Unknown JAXB
exception; nested exception is javax.xml.bind.PropertyException: name:
com.sun.xml.bind.namespacePrefixMapper value:
es.fs.fswarehousing.gls.ws.EmptyNamespacePrefixMapper#17f6a8fe
Can someone give me any hint :) ?

Related

Can not validate XML

I am trying to create a camel route in spring boot. I get the following error.
Caused by: org.xml.sax.SAXParseException: s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw '301 Moved Permanently'.
My applicationContext.xml looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws"
xmlns:cxf="http://cxf.apache.org/blueprint/core"
xmlns:camel="http://camel.apache.org/schema/blueprint"
xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd"
default-activation="lazy">
<!-- CXF SERVER -->
<camelcxf:rsServer id="productRestController"
address="http://0.0.0.0:8080/product"
serviceClass="com.born.oktopus.product.controller.ProductRestController"
loggingFeatureEnabled="true" loggingSizeLimit="20" >
<!-- <cxf:schemaLocations> <value>classpath:/schemas/productDataList.xsd</value>
</cxf:schemaLocations> <camelcxf:providers> <ref component-id="jaxbProviderXsi"
/> </camelcxf:providers> -->
</camelcxf:rsServer>
<!-- ERP BEANS -->
<bean id="loggingOutInterceptor"
class="org.apache.cxf.interceptor.LoggingOutInterceptor" >
<property name="prettyLogging" value="true" />
</bean>
</blueprint>
It sounds like you are trying to validate a transformation file with an XML file instead of an XSD as the applicationContext.xml appears correct.
You need to show us the JAVA code where you are parsing with SAX, but I would guess you either have a typo of some kind or you are thinking you are parsing the XSD file, but you reference the XML file.

How do you know the actually value for a property set by a map of beans on java?

Ive found this configuration on a applicationContext xml and I still dont understand it (even if I found info about it, is not clear) could someone give me an idea of how java chooses the right bean for its property? I did not find this bean in other file of the project context, seems like is "magic"
<bean id="mailCreator" class="com.project.ConfigurableXferEmailCreator">
<property name="mailCreatorMap">
<util:map id="mailCreatorMap" key-type="java.lang.String"
value-type="com.project.BaseMailCreator">
<entry>
<key>
<util:constant static-field="com.project.creatorType.TYPE1"/>
</key>
<bean class="com.project.creator1" parent="baseCreator">
<property name="service" ref="someService1" />
</bean>
</entry>
<entry>
<key>
<util:constant static-field="com.project.creatorType.TYPE2"/>
</key>
<bean class="com.project.creator1" parent="baseCreator">
<property name="service" ref="someService2" />
</bean>
</entry>
.... and so on
I really have no idea how java recognizes which one will use, I just know it uses the right service but I dont see where is being specifically set, could someone give me a hand?
I checked couple of sites like this but still no idea , does it call all services??
http://www.java2s.com/Tutorials/Java/Spring/0140__Spring_Map_Properties.htm
Your question:
"how java chooses the right bean for its property"
I assume you mean for example this line:
<property name="service" ref="someService1" />
if you do not see another XML bean element that defines "someService1"
fr example:
<bean id="someService1" class="com.project.SomeService1Impl">
Then it exists in the Java Code
try to find an annotation like below
#Service("someService1")
public class SomeService1Impl {
...
How?:
Spring loads "someService1" in the Spring Context on initialization , so the XML can reference it.

Spring Context: It's possible to define variables (not properties) in a XML and use it in runtime to obtain specific referenced beans?

I don't have much experience working with Spring Context and I don't know if this is possible...I'm trying to set into a Spring XML file a variable to define a bean reference (not a property).
Now I have a specidific xml:
keyIntegrator-key1.xml
<import resource="classpath:/events/key-events.xml" />
<context:annotation-config />
<bean id="keyIntegrator" class="com.emulated.KeySimulator" >
<property name="readList">
<list>
<bean class="com.emulated.ListEventGenerator">
<property name="eventList">
<ref bean="key-1-ok"/>
</property>
</bean>
</list>
</property>
</bean>
All the keys were defined in another xml file (key-events.xml).
I have to load in Java runtime the bean "keyIntegrator" with only one key, that is a input parameter in the Java program (I use the param to decide the xml file to load)
My question is if it's possible to define a variable inside the xml file and get the referenced bean using this variable:
Something like this:
keyIntegrator-generic.xml
<import resource="classpath:/events/key-events.xml" />
<context:annotation-config />
<bean id="keyIntegrator" class="com.emulated.KeySimulator" >
<property name="readList">
<list>
<bean class="com.emulated.ListEventGenerator">
<property name="eventList">
<ref bean="key-{inputKeyParam}-ok"/>
</property>
</bean>
</list>
</property>
</bean>
In the Java program I will need to pass the param to get the bean, something like this:
keySimulatorBean = (KeySimulator) context.getBean("keyIntegrator", "1");
There any way possible to make this ?
Thank you very much!
Thank you very much, it worked for me :)
I setted the system property value in the Java code:
System.setProperty("inputKeyParam", "1");
It is possible to do using Spring Expression Language.
For example reference to the bean can be defined using system property
<ref bean="key-#{systemProperties.inputKeyParam}-ok"/>
It will allow to reference different beans depending on provided VM option value, e.g. -DinputKeyParam=1

Issue during the spring upgrade from 1.2 version to 4.1.1

Below is the issue I am facing during starting up of the server.
org.springframework.web.context.ContextLoaderListener failed: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.net.ConnectException: Tried all: '2' addresses, but could not connect over HTTP to server: 'www.springframework.org', port: '80'
I thought that the issue might be with the xsd.I had made the respective change but still unable to resolve it.Can anyone help me out in solving the problem
Here's my application context xml file
**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="cdqiPerformanceMonitor"
class="com.tms.cdqi.framework.aop.interceptor.CDQIPerformanceMonitorInterceptor" />
<bean id="cdqiApplicationContextFactory"class="com.tms.cdqi.framework.context.CDQIAppl icationContextFactoryImpl" />
<bean id="commandInvoker"class="org.springframework.ejb.access.LocalStatelessSessionPr oxyFactoryBean"
lazy-init="true">
<property name="jndiName">
<value>ejb/CommandInvokerLocal</value>
</property>
<property name="businessInterface">
<value>
com.tms.cdqi.application.command.invoker.CommandInvoker
</value>
</property>
</bean>
</beans>**
As Per Denium suggestion I replaced my context xml DTD with versionless XSD
Thanks Deinum

Castor custom collection field handler

I'd like to unmarshal the following xml to a MultiKeyMap:
<map>
<entry key1="foo" key2="foo">
<value property="val"/>
</entry>
<entry key1="bar" key2="bar">
<value property="val"/>
</entry>
</map>
I took a look through the Castor API and saw that there's a CollectionFieldHandler class, but I can't find any documentation on where I would need to register a custom collection handler.
First of all, collection isn't a map.
You can implement and register your own handler.
See http://www.castor.org/xml-fieldhandlers.html

Categories

Resources