Ignore transitive dependency in Maven


Maven helps to manage the dependency of JARS . Most of the time it may happen that same JAR file with different versions is being loaded because some other jar files have it as dependency and thus causing the conflicts .

Typical example is have log4j or slf4j jar with different version in spring-xx.jar and junit-xxx.jar . So when you load you classes an conflict may arise and because of incompatible version of same jar file . For e.g.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.2.1.RELEASE</version>
</dependency>

The above jar uses log4j 1.2.17 (you can check the dependency using mvn dependency:tree).

Consider this log4j jar file is conflicting with some other higher version of log4j which is used by other jar. To resolve this issue we would have to remove the conflicting log4j jar from org.springframework.
it can be done as follows :

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.2.1.RELEASE</version>
         <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
</dependency>

Cheers..

Convert JSON to Map or Map to Json in Java

Tags

, , ,


One of the common uses case is to work with Json and use or convert it as java object.

Add the Jackson dependency in pom.xml file

<repositories>
	<repository>
		<id>codehaus</id>
		<url>http://repository.codehaus.org/org/codehaus</url>
	</repository>
  </repositories>
 
  <dependencies>
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
		<artifactId>jackson-mapper-asl</artifactId>
		<version>1.9.12</version>
	</dependency>
  </dependencies>

The Java class :

public static void main(String args[]) {
        ObjectMapper mapper = new ObjectMapper();
        Map m = new HashMap();
        Map personMap = new HashMap<>();
        Map personDetail = new HashMap();
        personDetail.put("firstname", "Bob");
        personDetail.put("lastname", "jackson");
        personDetail.put("age", "12");
        personDetail.put("city", "Berlin");
        
        personMap.put("person", personDetail);
        //convert Map to json string
        try {
            System.out.println(mapper.writeValueAsString(personMap));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // convert json to Map 
        String json = "{\"person\":{\"age\":\"12\",\"lastname\":\"jackson\""
                + ",\"firstname\":\"Bob\",\"city\":\"Berlin\"}}"
        try {
            Map map = mapper.readValue(json, Map.class);
            System.out.println("Map is " + map);
        } catch (Exception e) {
            e.printStackTrace();
        }
 }

Output is

{“person”:{“age”:”12″,”lastname”:”jackson”,”firstname”:”Bob”,”city”:”Berlin”}}
Map is {person={age=12, lastname=jackson, firstname=Bob, city=Berlin}}

Rails tip #1 – Set a custom layout for a web page

Tags


Its may require that you dont want to use application.html layout for some pages . For e.g display different header on page2 which than page 1 and the header is rendered in application.html.erb.

One way is to create a new layout for e,g headerless.html.erb inside a views->layout folder and in the particular controller which serves the request set the new layout .


 class SomeController < ApplicationController

    layout :headless , :only=>[:your_action1,:your_action2]

end

With the layout keyword you can use options such as :only, :except.

Follow

Get every new post delivered to your Inbox.

Join 107 other followers