why Spring 3.x path param is truncated - spring

The path param value is truncated randomly.
ex : myapplication/{pathparam}
Actual value "ab-cd-626-1.2.3.4 .6" is actually parsed as ab-cd-626-1.2.3.4.
No idea why this is happening, please help

I am not shure where you use that pathparam, may be in a #RequestMapping of a controller method ?
I experienced problems,when the path param contains '.', so I avoid that at all. The problem stems from the way, the url is parsed by spring.
More discussion on that : pathvariable with dot is getting truncated

<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
<mvc:annotation-driven
content-negotiation-manager="contentNegotiationManager">
<mvc:path-matching suffix-pattern="false" registered-suffixes-only="true" />
</mvc:annotation-driven>

Related

Spring configuration: TypeMismatchException

Using Spring 3.2 and Quartz 1.8. I've configured a org.springframework.scheduling.quartz.JobDetailBean as follows:
<bean id="a" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.MyJob" />
...
</bean>
I'd like to change this configuration so that the jobClass refers to a bean instance so that I can set some properties on the bean:
<bean id="b" class"com.MyJob">
<constructor-arg name="arg" value="1"/>
</bean>
<bean id="a" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" ref="b" />
...
</bean>
When launching the app with this config, I get
org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'com.MyJob' to required type 'java.lang.Class' for property 'jobClass'.
Why is that? I assume it's because the jobClass property requires a class and not an instance, so how do I get around that?
Found a solution. You can inject some properties into the jobDataAsMap, which in turn injects them into setters into you jobClass (or can be retrieved programmatically from the JobExecutionContext.jobDetail in your jobClass).
<bean id="a" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.MyJob" />
<property name="jobDataAsMap">
<map>
<entry key="propA" value="10" />
<entry key="propB" value="3" />
</map>
</property>
</bean>

For XML file change prefix and suffix of InternalResourceViewResolver to resolve XML file support

I am using InternalResourceViewResolver for resolving file names. Till now I was using only JSP so it was completely fine. Now for few requests I need to send xml files residing in my WEB-INF folder, but not getting idea how to exclude or include support these XML files
My controller is like this:
#RequestMapping(value = "/sitemap/sitemap_index.xml", method = RequestMethod.GET)
public String viewXmlSitemapIndex(ModelMap model) {
return "/other/sitemap_index"; //sitemap_index.xml is actual xml file residing in other folder inside WEB-INF
}
And in dispatcherservlet I have defined InternalResourceViewResolver like this:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
I guess you should use a ContentNegotiatingViewResolver like this
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="ignoreAcceptHeader" value="true" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="prefixJson" value="true" />
</bean>
</list>
</property>
</bean>
Here you can define multiple view resolver and you can choose the one to use for the XML
I hope this can help
Angelo

Spring Batch: Reading a File : if field is empty setting the default value

I am very new to spring batch. I have requirement in which i have to read a file having a header(Field Names) record and data records
i have to validate 1st record (check the field names matching against set of predefined names)- note that this record need to be skipped- i mean should not be part of items in processor)
read and store rest of the field values to a POJO
if the field 'date' is empty , i need to set the default value as 'xxxx-yy-zz'
i am unable to 1st and 3rd requirement with batch
here is the sample reader XML. please help
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="classpath:input/import" />
<property name="encoding" value="UTF-8" />
<property name="linesToSkip" value="1" />
<property name="lineMapper" ref="line.mapper"/>
</bean>
<bean id="line.mapper" class="org.springframework.batch.item.file.mapping .DefaultLineMapper">
<property name="lineTokenizer" ref="line.tokenizer"/>
<property name="fieldSetMapper" ref="fieldSet.enity.mapper"/>
</bean>
<bean id="line.tokenizer" class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer">
<property name="delimiter">
<util:constant static-field="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer.DELIMITER_TAB"/>
</property>
<property name="names" value="id,date,age " />
<property name="strict" value="false"/>
</bean>
<bean id="fieldSet.enity.mapper" class="org.springframework.batch.item.file.mapping .BeanWrapperFieldSetMapper">
<property name="targetType" value="a.b.myPOJO"/>
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertyeditors.C ustomDateEditor">
<constructor-arg>
<bean class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-mm-dd" />
</bean>
</constructor-arg>
<constructor-arg value="true" />
</bean>
</entry>
</map>
</property>
Create your own custom FieldSetMapper like below
CustomeFieldSetMapper implements FieldSetMapper<a.b.myPOJO> {
#Override
public a.b.myPOJO mapFieldSet(FieldSet fs) {
a.b.myPOJO myPOJO = new a.b.myPOJO();
if(fs.readString("date").isEmpty()){
myPOJO.setDate("xxxx-yy-zz");
}
return a.b.myPOJO;
}
}
You think you should do date set in ItemProcessor.
Also, if <property name="linesToSkip" value="1" /> not fill your requirements - extend FlatFileItemReader and validate first line manually in it.

How does one make Spring read from a file to string property?

I have a bean that has a string property that I would like to set, but I would like it set from a file without changing the bean code. The bean is something like this.
public class SomeBean {
public void setSomeProperty(String string) { ... }
}
I was looking for something like this in the beans.xml file
<beans>
<bean class="SomeBean">
<property name="someProperty">
<util:string src="classpath:foo.txt" />
</property>
</bean>
</beans>
Try using the PropertyPlaceholderConfigurer to load a value from a properties file:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:foo.properties</value>
</list>
</property>
</bean>
<bean class="SomeBean">
<property name="someProperty" value="${myBean.someProperty}" />
Then, in the foo.properties file, you set the property to whatever value you want:
myBean.someProperty = value
Hope this helps
I found a way using the Guava classes though it looks really bad.
(The value attribute is all in one line)
<property name="someProperty"
value="#{ T(com.google.common.io.Resources).toString(
T(com.google.common.io.Resources).getResource('foo.txt'),
T(java.nio.charset.Charset).forName('UTF-8')) }"/>
Hopefully someone can find a better answer.

CXF - com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 5))

I found on internet that problem is that soap request contain unicode char for ,,ctrl + v", which is illegal character in Xml. I dont know how this get into String, but I want simple to remove it on server side.
Can plase someone give me the point how to solve this issue?
I found this snippet :
XMLOutputFactory f = new WstxOutputFactory();
f.setProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
new InvalidCharHandler.ReplacingHandler(' '));
XMLStreamWriter sw = f.createXMLStreamWriter(...);
Can someone tell me how to configure Spring for construction of WstxOutputFactory with this handler? - InvalidCharHandler.ReplacingHandler(' '). Thanks for advice.
The solution is pretty simple :
<jaxws:endpoint id="kservice"
implementor="#kostrounService"
address="/call_kostroun" >
<jaxws:properties>
<entry key="javax.xml.stream.XMLOutputFactory" valueref="xmlOutputFactory" />
</jaxws:properties>
</jaxws:endpoint>
<bean id="invalidCharHandler" class="com.ctc.wstx.api.InvalidCharHandler$ReplacingHandler">
<constructor-arg value=" "/>
</bean>
<bean id="xmlOutputFactory" class="com.ctc.wstx.stax.WstxOutputFactory"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="xmlOutputFactory" />
</property>
<property name="targetMethod">
<value>setProperty</value>
</property>
<property name="arguments">
<list>
<util:constant static-field="com.ctc.wstx.api.WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER"/>
<ref bean="invalidCharHandler" />
</list>
</property>
</bean>
This snippet of configuration remove illegal characters from soap message, and app then run ;-)

Resources