Jackson helps you to "Ajaxify" your Spring service in less than 5 mins

0
You can now turn your Spring service into an ajax service easily with the use of annotation and Jackson

Let's say you have a service to search for a product, something like this:

public class productServiceImpl{

private ProductDao productDaoImpl;

public List search(String searchString){
productDao.search(searchString);
}

}

And usually your Spring MVC controller will be something like this

@Controller
@RequestMapping("/product")
public class ProductController{

private ProductService productService;

@RequestMapping("/search")
public List search(@RequestParam("searchString") String searchString, ModelMap model) {
return productService.search(searchString);
}

To expose the search service as an ajax service, simply
1. Add jackson to your library if you haven't do so
2. Add to your applicationContext.xml
Remember also add the schema Location
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
to specify the name space
3. add @ResponseBody before the return type in the method as follow

@RequestMapping("/jsonsearch")
public @ResponseBody List search(@RequestParam("searchString") String searchString, ModelMap model) {
return productService.search(searchString);
}

That's it!

To access this simply send request to
http://localhost:8080/myapp/product/search?searchString=iphone




Could not parse configuration: /hibernate.cfg.xml Caused by: org.dom4j.DocumentException: Connection refused

1

If you every run into the below,

org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
 at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1500)
 at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
 at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
 at org.hibernate.tutorial.hbm.NativeApiIllustrationTest.setUp(NativeApiIllustrationTest.java:47)
 at junit.framework.TestCase.runBare(TestCase.java:132)
 at junit.framework.TestResult$1.protect(TestResult.java:110)
 at junit.framework.TestResult.runProtected(TestResult.java:128)
 at junit.framework.TestResult.run(TestResult.java:113)
 at junit.framework.TestCase.run(TestCase.java:124)
 at junit.framework.TestSuite.runTest(TestSuite.java:243)
 at junit.framework.TestSuite.run(TestSuite.java:238)
 at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
 at org.dom4j.io.SAXReader.read(SAXReader.java:484)
 at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1490)
 ... 17 more

it means that the XML parser is having a problem trying to access your DTD definition. So first thing to do is to see if you can browse the url for it directly and also check your proxy settings. In my case, I changed it

from
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

to

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

I think you can also try using "http://www.jboss.org/dtd/hibernate/hibernate-configuration-3.0.dtd". It seems like hibernate.org fwds to the jboss location anyways.

(as of today....unless the urls change again)