7

I'm using templateRest to post User object to Rest Server but I encoutered the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException at edu.java.spring.service.client.RestClientTest.main(RestClientTest.java:33) Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

Here is the file RestClientTest.java

public class RestClientTest {

    public static void main(String[] args) throws IOException{
        RestTemplate rt = new RestTemplate();
//      System.out.println("Rest Response" + loadUser("quypham"));
//      URL url = new URL("http://localhost:8080/rest/user/create");
        rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        rt.getMessageConverters().add(new StringHttpMessageConverter());
//      Map<String,String> vars = new HashMap<String,String>();

        User user = new User();
        user.setUserName("datpham");
        user.setPassWord("12345");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,1960);
        user.setBirthDay(calendar.getTime());
        user.setAge(12);
        String uri = new String("http://localhost:8080/rest/user/create");
        User returns = rt.postForObject(uri, user,User.class);

//      createUser(user);
        System.out.println("Rest Response" + loadUser("datpham"));
    }

Here is the file UserRestServiceController.java

@Controller
public class UserRestServiceController {
    @Autowired
    public  UserDao userDao;
    @RequestMapping(value = "/rest/user/create",method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void addUser(@RequestBody User user){
        userDao.save(user);
    }

Here is the file pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.java.spring.service</groupId>
  <artifactId>springDAT-service</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springDAT-service Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.12.1.1</version>
    </dependency>

    <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>


  </dependencies>
  <build>
    <finalName>springMOTHER-service</finalName>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                    <argLine>-Xmx2524m</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <fork>true</fork>
                    <compilerArgs>
                        <arg>-XDignore.symbol.file</arg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.0.M1</version>
                <configuration>
                    <jvmArgs>-Xmx1048m -Xms536m
                        -XX:PermSize=128m -XX:MaxPermSize=512m</jvmArgs>
                    <reload>manual</reload>
                    <systemProperties>
                        <systemProperty>
                            <name>lib</name>
                            <value>${basedir}/target/spring-mvc/WEB-INF/lib</value>
                        </systemProperty>
                    </systemProperties>
                    <scanIntervalSeconds>3</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                    <contextPath>/</contextPath>
                    <webAppSourceDirectory>${basedir}/src/main/webapp</webAppSourceDirectory>
                    <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
                    <classesDirectory>${basedir}/target/classes</classesDirectory>
                </configuration>
            </plugin>

        </plugins>

  </build>
</project>

I have browsed information on google, afterwards I have tried to add jackson 2.4 in pom.xml but didn't seem to work:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.0-rc3</version>
</dependency>

2 Answers 2

12

Add this :

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>${jackson.version}</version>
</dependency>

You can replace ${jackson.version} with any version you want,or just add <jackson.version>2.x.x</jackson.version>at top of your pom.xml

p/s:Mine is version 2.7.3

Sign up to request clarification or add additional context in comments.

8 Comments

i add this dependencies in pom.xml. after i mvn jetty:run then occur error:[ERROR] Failed to execute goal on project springDAT-service: Could not resolve d ependencies for project edu.java.spring.service:springDAT-service:war:1.0-SNAPSH OT: Failed to collect dependencies at com.fasterxml.jackson.core:jackson-core:ja r:2.7.3: Failed to read artifact descriptor for com.fasterxml.jackson.core:jacks on-core:jar:2.7.3:
Could not transfer artifact com.fasterxml.jackson.core:jackso n-core:pom:2.7.3 from/to central (https://repo.maven.apache.org/maven2): C:\Prog ram Files (x86)\apache-maven-3.3.9\REPO\com\fasterxml\jackson\core\jackson-core\ 2.7.3\jackson-core-2.7.3.pom.part.lock (The system cannot find the path specifie d) -> [Help 1]
Do you update maven after that? Right click project->Maven->Update Project and press ok.
Downloading: repo.maven.apache.org/maven2/com/fasterxml/jackson/core/jac kson-core/2.7.3/jackson-core-2.7.3.pom [WARNING] Failed to create parent directories for tracking file C:\Program Files (x86)\apache-maven-3.3.9\REPO\com\fasterxml\jackson\core\jackson-core\2.7.3\jac kson-core-2.7.3.pom.lastUpdated
that because you put it in windows directory, you should put it in .m2 repository under user profile,and that should work. Change your maven setting at settings.xml
|
-2

Try add to your pom.xml

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

2 Comments

i have tried with your way, but still occur similar error.
This does not help, I have jackson-core-asl already, without success. The JsonProcessingException in jackson-core-asl is in another package: org.codehaus.jackson.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.