SpringData and MongoDB configuration error : MongoTemplate - spring

I am currently attempting an integration of mongodb to my spring application.
I have the initial mongodb environment setup on my machine, service startup, etc.
However on the application configuration in spring there is an error related to the mongoTemplate construction-arg
"wildcard is strict but no declaration found for element"
I've actually used the same xml outlay as described in the documents here, the same as some recent tutorials online, and as far as I can tell I have addded the correct namespace urls. So i am a bit lost at the moment as to what I am missing. Any help on this is most appreciated.
Here is a look at the xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- scanning comment root context of all components package directory-->
<context:component-scan base-package="com.demo" />
<!--Dispatcher servlet-->
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<!-- mongo db config -->
<mongo:mongo host="localhost" port="27017" id="mongo" />
<mongo:repositories base-package="com.demo.football.repository" />
<beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="FootballManager"/>
</beans:bean>
</beans:beans>

Qualify the xsd names to match the spring version you're using.
Considering you're using Spring 4, the xshema name space will change to following.
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd">

Related

Methods of JedisConnectionFactory is deprecated. Which XML configuations to used in Spring Batch?

I am developing an utility in Spring batch will read data from Mysql/Oracle and write it to the Redis database.
Currently we still using Spring Batch XML based configurations (we still like the XML based - gives little control to us :))
When I configured the following I see most of the methods are deprecated.
<bean id="redisDataSource" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name=""></property>
</bean>
using following versions of dependencies:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-batch-vesion>4.0.1.RELEASE</spring-batch-vesion>
<mysql.version>8.0.11</mysql.version>
<logback.version>1.2.3</logback.version>
<jcl.slf4j.version>1.7.25</jcl.slf4j.version>
<quartz.version>2.2.1</quartz.version>
<spring.version>5.0.0.RELEASE</spring.version>
<lombok.version>1.18.0</lombok.version>
<jedis.version>2.9.0</jedis.version>
</properties>
Could anyone please suggest XML based configurations which I should used to configured the dataSource ?
I've taken a reference from the link: https://docs.spring.io/spring-data/redis/docs/2.0.8.RELEASE/reference/html/, but its not clear to me.
STS snippet:
After lot of research and googling found the below useful links :
Weird redis key with spring data Jedis
Spring Data RedisTemplate: Serializing the Value and HashValue
https://github.com/hantsy/spring-sandbox/blob/master/spring-cache/src/main/java/com/hantsylabs/example/spring/config/applicationContext-jpa-redis.xml
Get Set value from Redis using RedisTemplate
Here is the code snippet:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="200" />
<property name="maxIdle" value="50" />
<property name="maxWaitMillis" value="3000" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis_ip}" />
<property name="port" value="${redis_port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true" />
</bean>
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisFactory" p:valueSerializer-ref="stringRedisSerializer"
p:keySerializer-ref="stringRedisSerializer" />
</beans>
Or Simply used https://docs.spring.io/spring-integration/reference/html/redis.html

My bootstrap style is not rendering prperly in my spring mvc application

Iam developing a E-musicstore application using spring mvc and hibernate.When I add header.jsp file to my ViewproductList.Jsp file The page is not rendering my bootstrap style properly.
my dispatcher servlet configuration
this is how its displayed in browser
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Interceptor -->
<context:component-scan base-package="com.mymusic" />
</beans:beans>
Try this... if you face same issue, please attatch your brower's NETWORK Tab which i can see HTTP information.

How does spring detects current persistenceUnitName?

I'm working on a spring and JPA project. I had configured my JPA Persistence Unit in the Persistence.xml and here's my spring configuration file.
My application works fine, but I didn't understand how does spring framework detects the Persistence Unit defined in my Persistence.xml file and injects it without being defined in my spring bean configuration file .
Can anybody answer me please ?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="ma.professionalpartners.fireAppBusiness.dao"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fireApp-Domain" />
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="jpaTransactionManager" />
</beans>
You have provided the name for the persistence unit, when configuring the entityManagerFactory bean:
<property name="persistenceUnitName" value="fireApp-Domain" />
The persistence.xml file MUST be on certain paths, so that Spring simply searched in those locations. After finding the file, it parses the XML content, and if there is a single PersistenceUnit, that is made the default one. Of course, if you specify a name (as you did), then it looks exactly for that PersistenceUnit.

MongoDB database creation with spring data

I am creating a sample web application with spring 3.0 and mongoDB. Following is my root-context.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<import resource="classpath:mongo-config.xml"/>
</beans>
and my mongo-config.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- Default bean name is 'mongo'. write concern set to SAFE to ensure unique indexes -->
<mongo:mongo host="localhost" port="27017" write-concern="SAFE"/>
<mongo:repositories base-package="com.dashboard.repositories" />
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
<constructor-arg name="mongo" ref="mongo"/>
<constructor-arg name="databaseName" value="DASHBOARD"/>
<constructor-arg name="credentials" ref="userCredentials"/>
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<property name="writeResultChecking">
<util:constant static-field="org.springframework.data.mongodb.core.WriteResultChecking.EXCEPTION" ></util:constant>
</property>
</bean>
<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="test"/>
<constructor-arg name="password" value="abc123"/>
</bean>
</beans>
when i start the tomcat server, there is no database "Dashboard" created. What is wrong with this code, Kindly help me. Thanks in advance.
For making it visible to other viewers i will put my comment into the answer. In Mongo schemas are dynamic and heterogenous so mongo cannot create a fixed schema as RDMS before inserting any data. Here is a more detailed explanation.

Spring MVC url-mapping

I made a simple web application by Spring mvc.
I want to use these URL
/user
/user/{id}
/user/create
/user/edit/{id}
in web.xml
first case
<servlet-mapping>
<servlet-name>SpringMVC1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
It works well.
but I can not read
http://localhost:8080/res/images/image.png - 404 error
in {my project path}/WebContent/res/images/logo.png
second case
<servlet-mapping>
<servlet-name>SpringMVC1</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I can see image on http://localhost:8080/res/images/image.png
but http://localhost:8080/user/create - 404 error
What's wrong??
You need something like this in your XML:
<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>
See 16.14.5. Configuring Serving of Resources
more detail explain..
in my spring configuration xml file
i append
<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>
it have to append next..
append to root node - beans
xmlns:mvc="http://www.springframework.org/schema/mvc"
and append to xsi:schemaLocation
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
and append mvc:annotation-driven node.
<mvc:annotation-driven />
It is my spring configuration xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/res/**" location="/res/" />
</beans>
It works well.
Thanks Sean Patrick Floyd.

Resources