archive-se.com » SE » S » SAWANO.SE

Total: 50

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • sôft'wâr': Scala + Spring MVC = True? (Part 3)
    NotNull and Min annotations on our ScalaIndata class will only be applied to the actual constructor parameters and not to any accessor methods or fields This holds true for any annotation for that matter and not just the ones we are using in this example And since the JSR 303 spec wants the annotations to be on either the accessor methods or the fields nothing will be picked up by the validator But do not despair Scala still has some cards up its sleeve that will help us solve this One such card is called meta annotations What a meta annotation allows you to do is to actually annotate the annotation An example of a meta annotation is beanGetter What beanGetter does is it says that the meta annotated annotation should be applied only to the getter method generated by the Beanproperty annotation Notice that we actually lose the default behavior here and if we still want the constructor parameter to be annotated we also would have to add a param as well To illustrate this here is what our ScalaIndata class will look like with meta annotations case class ScalaIndata BeanProperty NotNull beanGetter val name String BeanProperty Min beanGetter 1 val age Integer So with the meta annotations the NotNull annotation will be applied to the getName method and the Min annotation to the getAge method and we will have a Scala class that can be JSR 303 validated Something to consider here is that in some cases the meta annotations might make the code a bit messy and on the borderline of what can be considered to be readable code Imagine if we needed even more annotations for other purposes then there would be more annotations than code in this example An alternative to all these annotations would be to simply make the class follow the Java Bean convention and define the getters explicitly and then annotate the methods themselves Like this case class ScalaIndata val name String val age Integer NotNull def getName name Min 1 def getAge age This to me is a very straight forward and readable piece of code compared to a solution where we try to do everything in the constructor The take away here is that just because we can do something in a one liner does not mean that we always should Use your own judgment for which approach is most appropriate in your case Conclusion In this series of posts we have looked at how we can use Java Spring MVC and Scala together We have seen that there is no difference in creating Spring MVC controllers in Scala compared to Java and we have also seen that when bringing in additional Java frameworks we sometimes need to slightly adjust the Scala code to ensure compatibility Depending on your needs the more advanced you get and the more frameworks and features you use the more likely you are to run into compatibility issues As so often the KISS principle applies

    Original URL path: http://software.sawano.se/2012/05/scala-spring-mvc-true-part-3.html (2016-05-02)
    Open archived version from archive


  • sôft'wâr': Scala + Spring MVC = True?
    The project will be set up just as any standard Maven project but we need to add some extra configuration for the Scala compiler And of course we also need to add a dependency to the Scala libraries The configuration for the maven scala plugin that takes care of compiling the Scala source looks like this plugin groupId org scala tools groupId artifactId maven scala plugin artifactId version 2 15 2 version executions execution id scala compile first id phase process resources phase goals goal add source goal goal compile goal goals execution execution id scala test compile id phase process test resources phase goals goal testCompile goal goals execution executions configuration scalaVersion scala version scalaVersion configuration plugin The newer version of the plugin is now called scala maven plugin but at the time of writing IntelliJ 11 1 had problems detecting the Scala source folders with it so I decided to use the older version for this example The default folder structure for the Scala source is src main scala and src test scala If you download the example code you will see some additional configuration that enables us to run tests and integrations tests easily If you are new to Maven some basic commands that will get you started with playing around with the example code for this post are mvn clean test to run the unit tests and mvn clean verify to run the integration tests All Maven commands should be executed in the project s root folder where the pom xml file is located To run the integration tests we are using the Maven Failsafe Plugin The way this have been set this up is that when executing verify we start up an in memory Jetty instance deploys the web application runs the integration tests and then shuts down the Jetty instance Take a look at the example code if you want to see exactly how this is done The Failsafe plugin s default behavior is that any test classes whose names starts or ends with IT or ends with ITCase is considered an integration test This means that those tests are excluded when calling mvn test and included when calling mvn verify so you can use that naming convention to differentiate between unit tests and integration tests The Java tests are written in JUnit and the Scala tests are using Specs2 The basics Lets start out with the most basic example We want to be able to make a request to our web service and get a simple response back In other words if we do a GET http localhost 9090 scala spring mvc java ping we want the server to respond with the text pong A Spring MVC controller in Java for achieving this would look something like this RequestMapping java Controller public class JavaController RequestMapping ping ResponseBody public String ping return pong This will also work perfectly good for a controller written in Scala The Scala version of the same controller would look

    Original URL path: http://software.sawano.se/2012/04/scala-spring-mvc-true.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Scala + Spring MVC = True? (Part 2)
    extra JSON annotations are there because we made the POJO immutable If it had been mutable we would not need them But who wants mutable objects anyway If we just copy this over to Scala code we would end up with a controller method like this RequestMapping Array echo ResponseBody def echo name String ScalaEcho ScalaEcho name weekDay and the supporting class case class ScalaEcho JsonProperty name val name String JsonProperty weekDay val weekDay String Now here is where we run into our first problem If we would try to run this code it would not work because Jackson would not know what to do with the Scala objects returned by the controller methods This is because Jackson like many other frameworks are expecting classes to follow the Java Bean naming convention for accessor methods and Scala classes does not do that by default In this simple example we could solve this problem by annotating the class parameters with the Scala annotation BeanProperty This will tell the Scala compiler to generate accessor methods that follows the Java Bean convention and then the Jackson processor will work After annotating our case class it will look like this case class ScalaEcho JsonProperty name BeanProperty val name String JsonProperty weekDay BeanProperty val weekDay String But that approach has its limitations Especially when it comes to deserializing JSON to Scala classes If we for example adds a Tuple to our ScalaEcho class then the serialization process to JSON would work just fine but we would no longer be able to deserialize to ScalaEcho To exemplify this the test code below would fail during the deserialization process if we added a Tuple to the ScalaEcho class Test public void scalaEcho String weekDay new SimpleDateFormat E format new Date ScalaEcho echo createScalaTemplate getForObject baseUrl scala echo name name ScalaEcho class Daniel assertEquals Daniel echo name assertEquals weekDay echo weekDay An alternative solution to using Beanproperty is to enable Jackson to serialize and deserialize Scala classes To do this we need to use the Jackson module for Scala The module will provide serialization and deserialization of case classes sequences maps tuples options and enumerations But it does have some limitations and I would suggest checking out the documentation and trying out more advanced examples yourself if you decide to go all in for this solution You can refer to the example code for this post if you want to know how to register the module so that your message converter can use it Another effect of using the Scala module for Jackson is that now we can also remove the JSON specific annotations from our case class The case class after introducing the Scala module will now look just like an ordinary case class Nice and clean case class ScalaEcho val name String val weekDay String Once you have got Jackson working for processing Scala classes you can mix and match Java and Scala in your Spring MVC controllers For example you could have a Java controller

    Original URL path: http://software.sawano.se/2012/04/scala-spring-mvc-true-part-2.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': June 2015
    is no free lunch and these benefits do not come without a price An example of this is the fact that there are many challenges you may face when trying to implement continuous delivery in an existing organization My colleague Daniel Deogun and I have spent quite a lot of time working with implementing continuous delivery practices within organizations And in doing so we started to notice some reoccurring patterns among the challenges that organizations run into when moving towards continuous delivery Read more Posted by Daniel Sawano 2 comments Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Links to this post Labels Agile Continuous Delivery Conway s Law Organizational analysis Newer Posts Older Posts Home Subscribe to Posts Atom soft ware noun sôft wâr The programs and other operating information used by a computer About Me Daniel Sawano Developer architect public speaker agile dude and sporadic blogger Loves creativity beautiful software TDD BDD DDD and a whole bunch of other acronyms Tweets by DanielSawano Labels Java Spring Spring MVC JSON Jackson Maven Scala XML Clean Code JAXB REST RESTful Agile Akka Concurrency Continuous Delivery Conway s Law HATEOAS IntelliJ JPA JSR 303 Mockito Modeling Organizational

    Original URL path: http://software.sawano.se/2015_06_01_archive.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': March 2015
    for objects that had no intentions whatsoever to be a java bean And because it was such a widespread naming convention many frameworks and libraries started to rely on that convention in order to provide useful features Luckily many of the popular frameworks and libraries that are used today no longer require these naming conventions in order to function That means that today it is perfectly fine to write code like person name if it makes more sense to you and you will not be forced to write person getName just in order to get your tooling to work Read more Posted by Daniel Sawano 0 comments Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Links to this post Labels Clean Code IntelliJ Java Productivity Newer Posts Older Posts Home Subscribe to Posts Atom soft ware noun sôft wâr The programs and other operating information used by a computer About Me Daniel Sawano Developer architect public speaker agile dude and sporadic blogger Loves creativity beautiful software TDD BDD DDD and a whole bunch of other acronyms Tweets by DanielSawano Labels Java Spring Spring MVC JSON Jackson Maven Scala XML Clean Code JAXB REST RESTful Agile Akka

    Original URL path: http://software.sawano.se/2015_03_01_archive.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': April 2014
    like and you may not agree with all of them but the importance is to understand the overall concept and purpose of the term Other areas to explore in terms of code design is domain driven design DDD and test driven development TDD In my view well designed code is the foundation of everything that we do as software professionals All contemporary best practices that we apply daily are dependent of well designed and expressive code Whether it be TDD DDD domain driven security continuous integration scalability and so on A poorly designed code base that is nothing more than a big mess of code will never enable you to succeed in any best practice at all Period This may sound a bit harsh but in the long run I really do believe that well designed code is the base for everything else Read more Posted by Daniel Sawano 0 comments Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Links to this post Labels Clean Code Java Modeling Newer Posts Older Posts Home Subscribe to Posts Atom soft ware noun sôft wâr The programs and other operating information used by a computer About Me Daniel Sawano

    Original URL path: http://software.sawano.se/2014_04_01_archive.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': June 2013
    some sort that lends itself to parallelizing Implementing that with just core Java is cumbersome error prone and it will feel like you are reinventing the wheel every time you do it Akka will for example enable you to take advantage of all the cores in your CPU or maybe even the CPUs of several machines without having to worry about the evils of things like threads lock semaphores etc and not to mention the hassle of trying to distribute the computations Read more Posted by Daniel Sawano 0 comments Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Links to this post Labels Akka Concurrency Java Newer Posts Older Posts Home Subscribe to Posts Atom soft ware noun sôft wâr The programs and other operating information used by a computer About Me Daniel Sawano Developer architect public speaker agile dude and sporadic blogger Loves creativity beautiful software TDD BDD DDD and a whole bunch of other acronyms Tweets by DanielSawano Labels Java Spring Spring MVC JSON Jackson Maven Scala XML Clean Code JAXB REST RESTful Agile Akka Concurrency Continuous Delivery Conway s Law HATEOAS IntelliJ JPA JSR 303 Mockito Modeling Organizational analysis Productivity Spring Data TDD

    Original URL path: http://software.sawano.se/2013_06_01_archive.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': February 2013
    x at the time of writing this post Personally I have found that I hardly ever use any XML configuration files in Spring projects that I create nowadays And as a side note if I am also able to use an application server that supports Servlet 3 0 I can even get rid of my web xml file Thus eliminating a big part of the XML based configuration usually needed Ok but why You might ask Well if you think about it there are a lot of benefits of having your configuration in code instead of in XML Read more Posted by Daniel Sawano 0 comments Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Links to this post Labels Java Spring Spring MVC XML Newer Posts Older Posts Home Subscribe to Posts Atom soft ware noun sôft wâr The programs and other operating information used by a computer About Me Daniel Sawano Developer architect public speaker agile dude and sporadic blogger Loves creativity beautiful software TDD BDD DDD and a whole bunch of other acronyms Tweets by DanielSawano Labels Java Spring Spring MVC JSON Jackson Maven Scala XML Clean Code JAXB REST RESTful Agile Akka Concurrency

    Original URL path: http://software.sawano.se/2013_02_01_archive.html (2016-05-02)
    Open archived version from archive