2018-08-20

Spring JpaRepository Hibernate Jackson Junit Test, Controller.








Error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: xx.xx.xx.db.xx.xx.xx.xx, could not initialize proxy - no Session at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:588) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:207) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:151) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261) [hibernate-core-5.3.3.Final.jar:5.3.3.Final] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:97) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3893) [jackson-databind-2.9.5.jar:2.9.5] at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3207) [jackson-databind-2.9.5.jar:2.9.5]



Config to clear error:


Reference URL:
https://coderleaf.wordpress.com/2016/11/07/jackson-serialization-and-hibernate-lazy-loading/

@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport{     @Override    protected void extendMessageConverters( List<HttpMessageConverter<?>> converters ) {        for ( HttpMessageConverter<?> converter : converters ) {            if ( converter instanceof MappingJackson2HttpMessageConverter ) {                MappingJackson2HttpMessageConverter jacksonConverter = (MappingJackson2HttpMessageConverter) converter;                                ObjectMapper objectMapper = jacksonConverter.getObjectMapper();                                 //--- register hibernateModule in MappingJackson2HttpMessageConverter.objectMapper                objectMapper.registerModule(new Hibernate4Module());                 //--- other configurations                jacksonConverter.setPrettyPrint( true );                objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);                                objectMapper.enable(SerializationFeature.INDENT_OUTPUT);            }        }                 super.configureMessageConverters(converters);    } }






xml config to clear error

Reference URL:
https://stackoverflow.com/questions/43946323/spring-and-mappingjackson2httpmessageconverter-and-registermodule




<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<bean id="objectMapper"
    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">

    <property name="featuresToDisable">
        <array>
            <util:constant
                static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
        </array>
    </property>

    <property name="dateFormat">
        <bean class="java.text.SimpleDateFormat">
            <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
        </bean>
    </property>

    <property name="modulesToInstall"
        value="
        com.fasterxml.jackson.datatype.jdk8.Jdk8Module,
    com.fasterxml.jackson.datatype.jsr310.JavaTimeModule,
    com.fasterxml.jackson.module.paramnames.ParameterNamesModule" />
</bean>













2018-08-17

java tomcat session path



Case deploy to tomcat/webapps/projectA

but access to page by projectA.domain.com/

mean i have tomcat/webapps/projectA/gg.jsp

then deploy to server access by projectA.domain.com/gg.jsp

and facing session path issue.


Solutions refer to:
https://stackoverflow.com/questions/12755499/how-to-change-jsessionid-cookie-path-to-server-root-in-spring-app-on-jetty

<session-config>
    <session-timeout>15</session-timeout>
    <cookie-config>
        <name>JSESSIONID</name>
        <path>/</path>
        <http-only>false</http-only>
        <secure>false</secure>
    </cookie-config>
</session-config>








Reference URL:
https://www.mkyong.com/servlet/how-to-configure-the-session-timeout-in-servlet/

1) Timeout in the deployment descriptor (web.xml)
– Specified the timeout value in “minute” , enclose with “session-config” element.
<web-app ...> <session-config> <session-timeout>20</session-timeout> </session-config></web-app>CopyThe above setting is apply for the entire web application, and session will be kill by container if client doesn’t make any request after 20 minutes.




Reference URL:
https://stackoverflow.com/questions/40753753/cookies-with-path-path-and-jsessionid













2018-08-15

Stuck for 2.5 hrs...


codecommit
git

facing unable to push
keep commit everything
then pull
and face conflict
fix 1 by 1
then add fixed files into index, then commit and push again.



spring 注解 @ResponseBody 返回JSON 能不能设置他不返回为 null 的值


Searched by:
@responsebody without null value
https://www.google.com.sg/search?q=%40responsebody+without+null+value&oq=%40responsebody+without+null+value&aqs=chrome..69i64.7166j0j1&sourceid=chrome&ie=UTF-8


Reference URL:
https://segmentfault.com/q/1010000002522525


设置jackson忽略null

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="serializationInclusion">
                        <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>




2018-08-07

spring form checkbox auto add hidden input css not working










https://stackoverflow.com/questions/43749850/how-to-place-a-tag-element-between-the-input-tag-and-the-hidden-field


https://jsfiddle.net/DiogoBernardelli/rLutkcx0/

.switch input[type="checkbox"]:checked + input[type="hidden"] + label:after{
    left: 23px;
}
.switch input[type="checkbox"]:checked + input[type="hidden"] + label{
    background-color: #fff;
    color: #f00;
}


2018-08-04

log4j2 json jsn test maven pom






using json jsn for log4j2 configuration must add jackson-databind in maven pom.xml

log4j2 slf4j maven





See this?
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.





Add this

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.1</version>
</dependency>


If not found, then add repo

<repositories>
  <repository>
    <id>apache.snapshots</id>
    <name>Apache Snapshot Repository</name>
    <url>https://repository.apache.org/snapshots</url>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>

2018-08-01

java.lang.IllegalArgumentException: Attribute 'value' is required when binding to non-boolean values





java.lang.IllegalArgumentException: Attribute 'value' is required when binding to non-boolean values







Resource URL:

https://blog.csdn.net/goldenfish1919/article/details/7039104



2018-07-23

Spring Data JPA Junit test


Source URL: https://www.luckyryan.com/2013/06/25/integration-testing-spring-data-jpa/#comment-11973


All below need to be in place

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

<jpa:repositories base-package="com.techstudio.staypleased.db" />

<!-- id cannot use other value -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="entityManagerFactory" ref="entityManagerFactory" />






something is not correct in the post:
https://stackoverflow.com/questions/23435937/how-to-test-spring-data-repositories

To make it short - there's no way to unit test Spring Data JPA repositories reasonably for a simple reason...

2018-07-13

Maven pom.xml production deployment .




<!-- Maven compiler plugin -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${version.jdk}</source>
<target>${version.jdk}</target>
</configuration>
</plugin>


I think this [Maven compiler plugin] above will process files from /src/main/** to /target/




<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>config/**</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</configuration>
</plugin>


This [Maven jar plugin] above will process files from /src/main/** to /target/








Reference URLs:
  1. Move resource files to target and exclude from jar

    https://stackoverflow.com/questions/39705917/move-resource-files-to-target-and-exclude-from-jar
  2. How to Exclude properties file from jar file?

    https://stackoverflow.com/questions/2728993/how-to-exclude-properties-file-from-jar-file









2018-04-26

Jackson ObjectMapper writeValueAsString StackOverflowError



com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.vividsolutions.jts.geom.Point["envelope"]->           repeat 997 times           ->com.vividsolutions.jts.geom.Point["envelope"])



Working solution URL: https://github.com/bedatadriven/jackson-datatype-jts/issues/8





ObjectMapper om = new ObjectMapper();
om.registerModule( new com.bedatadriven.jackson.datatype.jts.JtsModule.JtsModule() );


2018-04-25

Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field


Environment:

JDK 1.7.0_80
Tomcat 8.0.36
MySql 5.5.50
Spring 4.3.16.RELEASE
Hibernate 4.3.11.Final

Hibernate Spatial 4.3


Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field

Getting fail to store Polygon Shape Object in Geometry Datatype with hibernate spatial API




Check column after insert:



SELECT AsText( geometry_column ) FROM table;

Google Referrals