Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Tuesday, 19 October 2010

Accessing Picasa's atom feed API from Spring's REST and XPath templates

Been swearing at my screen and Google's choice of atom based API for their Picasa web albums. Flickr's REST API took 10 seconds to work out. Picasa: days...


My problem:

Trying to parse album ids from Picasa's API: code.google.com/apis/picasaweb/docs/2.0/developers_guide_protocol.html#ListAlbum.


Source picasaResponse = restTemplate.getForObject( albumsURL, Source.class, parameters );

Jaxp13XPathTemplate xpathTemplate = new Jaxp13XPathTemplate();

Collection albumIds = xpathTemplate.evaluate("//entry",

      picasaResponse, new NodeMapper(){

   public Object mapNode(Node node, int i) throws DOMException {

      Element album = (Element) node;

      log.debug("IN NODE ALBUM");

      return null;

   }

}


This never worked. It should have I thought. Trying different combinations of "/entry", "/feed/entry", "//atom:entry", "/atom:feed/atom:entry", swapping with Jaxen, etc made no impact.


Then after some googlebashing and soulsearching I solved the problem.

The Xpath template needs a namespace to resolve the atom xml. So adding this solved the problem:


Source picasaResponse = restTemplate.getForObject( albumsURL, Source.class, parameters );

Jaxp13XPathTemplate xpathTemplate = new Jaxp13XPathTemplate();

Properties namespaces = new Properties();

namespaces.setProperty("atom",

   "http://www.w3.org/2005/Atom");

xpathTemplate.setNamespaces(namespaces);

Collection albumIds = xpathTemplate.evaluate("//atom:entry",

      picasaResponse, new NodeMapper(){

   public Object mapNode(Node node, int i) throws DOMException {

      Element album = (Element) node;

      log.debug("IN NODE ALBUM");

      return null;

   }

}




Ps. code simplified and not using bean injection etc for clarity.

Friday, 23 July 2010

Spring MVC ExceptionHandler needs to be in same class

Had a frustrating evening (pet project) trying to get Spring 3.0 MVC's annotation based exception handler to work.

All the examples seemed straight forwards:

Just annotate a method in a controller:
import org.springframe...ExceptionHandler;
import org.springframe...ModelAndView;
import org.springframe...Controller;
....
@Controller
public class MyController {
....
@ExceptionHandler(SomeException.class)
public ModelAndView myExceptionHandler(
    SomeException exception){
  blah
}
...
}


But it just would never catch my exceptions. Very frustrating.

But in the end a google search to the end of the world where someone mentioned in the comments that the exceptionhandler method have to be be in the same class as the method throwing the exception.

Very odd restriction compared to Spring's usual very agile and generic annotation.

So I added my handlers to my abstract controller class that all my controllers extend and problem solved, with some hair still intact.

Wednesday, 15 October 2008

groovy netbeans so far

been hacking away at groovy with netbeans as IDE for the past few weeks. Also using it with spring for a facebook application.

Well, I am still coding the java way... Hard to take the red pill. Groovy is nice, but not sure Ill use it for all coding in future applications. It can certainly be partly used, for often changed code. It may however replace my perl scripts.

As for netbeans and its groovy support. It does not seem finished: Its useable with neat tricks, but still fiddly, and some things just dont work. E.g creating a project from netbeans, then wouldnt let you add groovy classes to groovy folders only java source folders. Creating a project from scratch using maven archetype got working, but new java sources arent' happy then...

As in general use of netbeans it is very good, however a nightmare on a low memory machine, especially with encrypted disks. If I type one word, ill have to wait 10 secs before I can type the next word. Infuriating. So notepad++ is my general editor on that machine.

On a still low spec machine but without an encrypted disk it is better than eclipse. However all popups take 10secs before they finish "scanning", so they are irritating.

As for general development with groovy, it certainly increases developer velocity. However I keep flicking back to Java as certain mixture of architecture and technology is not quite there yet.

One thing that does work well with groovy and spring is spring's new annotation based configuration.

One thing I hope will soon work is dynamic beans, which spring has, but does not as yet work with annotations.

Friday, 19 September 2008

groovy spring annotation

Hmmm.

Groovy supports well spring's annotations.
Spring's dynamic language tags are also handy.

But I wish there was some dynamic language annotation!

As in eg, I have a Service written in Groovy such as:

@Service
class PersonService {

@Autowired
PersonRepository

blah blah ...

}

wouldn't be nice if I could:

@Service
@DynamicScript("Groovy")
class PersonService {

@Autowired
PersonRepository

blah blah ...

}

thus enabling it to be a refreshable bean without xml!

Tuesday, 22 April 2008

No single default persistence unit defined

If you getting nowhere with this type of problem:

Using spring and jpa, and you get this error:

No single default persistence unit defined in {classpath*:META-INF/persistence.xml}

And you DO have a persistence.xml in your classpath.

Then the cause is simple.

You may have other persistence.xml files as well in your lib/jars.

Solution, add your persistenceUnitName to entityManager bean.


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="blahblahYourName" />


which matches the one in your persistence.xml


<persistence-unit name="blahblahYourName">

</persistence-unit>

Wednesday, 7 March 2007

Spring

Held recently a presentation on Spring at TietoEnator and NetCom. It was a brief introduction to the Spring Framework and the IOC, MVC, JDBC, Hibernate, Acegi modules.

It was the same presentation , but the first one dragged on for 1.5 hours. At which point the audience was getting quite bored. The second wizzed past in 30 minutes. This time the audience probably did not catch a lot of what I said.

It is definetly not easy. Both times the projectors played up and caused a lot of grief. The first presentation included way too many examples. The second not enough.

If curious, the presentation is here, and all the examples are here.