And Todays’s Quote Would Be..

Life begins at the end of your comfort zone.

Neale Donald Walsch

Error “Can not write a field name, expecting a value;” while Writing JSON on Spring MVC

The error started since i upgraded my jackson library, somehow it never happen before. This is my full stacktrace

com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value
	com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1649)
	com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:186)
	com.edw.test.jackson.TestSerializer.serialize(TestSerializer.java:27)
	com.edw.test.jackson.TestSerializer.serialize(TestSerializer.java:19)
	com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
	com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1387)
	com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:889)
	org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:292)
	org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:106)
	org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:231)
	org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
	org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Looks like this error happen on my custom json serializer,

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" p:objectMapper-ref="testObjectMapper"/>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    <context:component-scan base-package="com.edw.test.jackson"/>
</beans>

And this is the content of my objectMapper java class,

package com.edw.test.jackson;

import com.edw.test.jackson.bean.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.stereotype.Component;

@Component
public class TestObjectMapper extends ObjectMapper {
    public TestObjectMapper() {
        super();
        
        SimpleModule module = new SimpleModule();
        module.addSerializer(Test.class, new TestSerializer());
        registerModule(module);
        configure(SerializationFeature.INDENT_OUTPUT, false);
    }
}

And as you can see, the main culprit is on class TestSerializer

package com.edw.test.jackson;

import com.edw.test.jackson.bean.Test;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;

public class TestSerializer extends JsonSerializer<Test> {

    @Override
    public void serialize(Test t, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeRaw("{");
        jgen.writeFieldName("test01");
        jgen.writeRaw(t.getTest01());
        jgen.writeFieldName("test02");
        jgen.writeRaw(t.getTest02());
        jgen.writeRaw("}");
        jgen.writeEndObject();
    }
}

replacing writeFieldName method with writeRaw method solve my problem.

package com.edw.test.jackson;

import com.edw.test.jackson.bean.Test;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;

public class TestSerializer extends JsonSerializer<Test> {
    @Override
    public void serialize(Test t, JsonGenerator jgen, SerializerProvider sp) throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeRaw("test01");
        jgen.writeRaw(t.getTest01());
        jgen.writeRaw("test02");
        jgen.writeRaw(t.getTest02());
        jgen.writeEndObject();
    }
}

Hope it helps :)

Google+

[Netbeans] Error NoClassDefFoundError when Building Java Project using Maven

I got this error when trying to build my maven project on Netbeans,

java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils

Here is my complete stacktrace,

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
	at org.apache.maven.wagon.providers.file.FileWagon.resolveDestinationPath(FileWagon.java:206)
	at org.apache.maven.wagon.providers.file.FileWagon.resourceExists(FileWagon.java:265)
	at org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.run(WagonRepositoryConnector.java:577)
	at org.sonatype.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:60)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
	at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
	... 7 more

Looks like it happen because my maven doesnt have a StringUtils class, from apache-commons-lang library. Adding commons-lang-2.6.jar to my Netbeans maven folder (E:\Program\NetBeans 8.2\java\maven\lib\ext) solves this issue.

Google+

Error “Expecting: TRUSTED CERTIFICATE” when Deploying .cer Certificate on Nginx

Got this very weird error when adding .key and .cer on my nginx SSL, “140387178489504:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:703:Expecting: TRUSTED CERTIFICATE”. It’s very weird because it works well previously.

At first i tought it was because i need to convert .cer to .crt, but even i cannot convert my .cer file using openssl. So i come to a conclusion that somehow there was an issue on my .cer file. First my .cer file looks fine when im opening it using vi

-----BEGIN CERTIFICATE-----
MIIF/jCCBOagAwIBAgIMUx0WMLOuFkHzf07EMA0GCSqGSIb3DQEBCwUAMGYxCzAJ
BgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMTwwOgYDVQQDEzNH
bG9iYWxTaWduIE9yZ2FuaXphdGlvbiBWYWxpZGF0aW9uIENBIC0gU0hBMjU2IC0g
RzIwHhcNMTcwMzIxMDM0NzAzWhcNMTkwMTI5MDIwMzI1WjCBozELMAkGA1UEBhMC

But when im open it using vi -b (vi -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@D^@V^@Q^@Q^@H^@D^@A^@V^@I^@Y^@W^@5^@v^@a^@T^@E^@W^@M^@B^@Q^@G^@A^@1^@U^@E^@C^@w^@w^@N^@S^@V^@Q^@g^@^M^@
^@R^@G^@V^@w^@Y^@X^@J^@0^@b^@W^@V^@u^@d^@D^@E^@8^@M^@D^@o^@G^@A^@1^@U^@E^@C^@g^@w^@z^@V^@k^@l^@F^@V^@E^@5^@B^@T^@U^@9^@C^@S^@U^@x^@F^@I^@F^@R^@F^@T^@E^@V^@D^@T^@0^@1^@N^@V^@U^@5^@J^@Q^@0^@F^@U^@^M^@
^@S^@U^@9^@O^@U^@y^@B^@K^@T^@0^

Somehow it happens because im transferring my .cer file between my windows desktop to my linux server environment. Using dos2unix command from my windows environment solve this issue,

C:\Users\edw>dos2unix C:\my.cer
dos2unix: converting UTF-16LE file C:\Users\edw\Downloads\selfcare.cer to ANSI_X
3.4-1968 Unix format...

ps.
i have dos2unix on my windows environment from cygwin installment.

Google+

Fixing MySql’s Error, “Cant create test file /folder/data/servername.lower-test” on RedHat OS

Had a very weird error starting my MySql after im moving MySql’s data folder from /var/lib/mysql to /folder/data on my RHEL 7 Server,

2017-03-23 11:50:52 2119 [Warning] Can't create test file /folder/data/servername.lower-test
2017-03-23 11:50:52 2119 [Warning] Can't create test file /folder/data/servername.lower-test
2017-03-23 11:50:52 2119 [ERROR] /usr/sbin/mysqld: Can't create/write to file 
    '/folder/data/servername.lower-test' (Errcode: 13 - Permission denied)

very very weird because im already change the ownership of my /folder data to mysql user, and still not working. I even chmod it to 777 for testing purpose, yet still no positive result. After a while i found out that my SELinux is blocking it, here is the command to unblock it.

[root@servername ~]# setenforce 0
[root@servername ~]# getenforce
Permissive

but the commands above only configure your SELinux until reboot. If you want to make it permanent, you could use this command,

[root@servername ~]# vi /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted  

Hope it helps others, cheers

Google+

How to Find Root Cause of Error 400 on SpringMVC using Log4J

Error 400 is basically telling us that our request is syntactically incorrect. But it will need a while to found out which parameter is incorrect, especially when you are submitting a lot of parameters.

But do not worry, on SpringMVC we can always log which parameters causing error 400 using Log4J. Adding these values on your log4j.properties will display the error location.

log4j.logger.org.springframework.web=debug

And we can see the root cause on our log file,

2017-02-28 22:33:07,241 [DefaultHandlerExceptionResolver] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver:133 - Resolving exception from handler [public java.util.Map xxx.yyy.controller.ZZZController.saveZZZ(xxx.yyy.bean.DDDD,org.springframework.web.multipart.MultipartFile,org.springframework.web.multipart.MultipartFile,javax.servlet.http.HttpSession)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'DDDD' on field 'rupiah': rejected value [100.000.000,000000]; codes [typeMismatch.DDDD.rupiah,typeMismatch.rupiah,typeMismatch.java.math.BigDecimal,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [DDDD.rupiah,rupiah]; arguments []; default message [rupiah]]; default message [Failed to convert property value of type 1 to required type 1 for property 'rupiah'; nested exception is java.lang.NumberFormatException]

As you can see, it happens because i expect a BigDecimal on my bean, but i send a String instead.

Google+