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?
    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?showComment=1334817783880 (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?showComment=1334820773493 (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Take your REST API to the next level with HATEOAS
    that data should be formatted so the server can understand it First off we need to provide some sort of template describing the data that can be POSTed and secondly we need a way to tell what template goes with what link The way we do this is by introducing the concept of commands We extend the links with a command attribute that specifies the name of the command that should be used with that link The command itself simply contains the template The link for entering a new order would look like this after applying a command to it rel add href http foo bar trading equities 123456 method POST command addOrder And the addOrder command will look like this commandName addOrder template Some template Now that we have the command structure in place we need to choose a format for the template that will describe the format of the POSTed data If our API was using HTML then this could be accomplished by returning a HTML form with all the required input fields etc However our API is using JSON so we cannot do that JSON Schema to the rescue By using a JSON Schema as our template we can describe the JSON data that should be POSTed and the client can use parse this schema on the fly and present a form to the user that allows her to input relevant fields The JSON Schema can for example also be used to describe what possible values that are valid for fields that have limited input options For example an order in our trading service can only be of one out of three different types Fill and Kill Fill or Kill and Good Til Cancelled The schema let us specify the possible values and the client could present these to the user as a drop down To put this in context here is how we would go about to enter a new order First we need to get to the instrument equity we want to trade in This is done by performing a search or viewing a list of all instruments From the search hit we get a link http foo bar trading equities 123456 that we can perform a GET on to get the details of the instrument For simplicity we assume that one instrument only have one orderbook that you can place an order in in reality there are several orderbooks but that is more domain detail than we need for this example The GET request would then return information about the instrument and a link to the URL for entering a new order instrument instrumentKey mic XNAS isin US0378331005 currency USD orderbookId 123456 longName Apple Inc shortName AAPL links rel self href http foo bar trading equities 123456 rel add href http foo bar trading equities 123456 method POST command addOrder commands commandName addOrder template type object properties size type number openSize type number price type number marketPriceOrder type boolean side type string enum BUY

    Original URL path: http://software.sawano.se/2012/04/take-your-rest-api-to-next-level-with.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Combining JSON and XML in RESTful web services with Spring MVC
    Running the code There are three integrations tests included in the source code in RESTControllerTestIT java that you can use to see how it all works To see it in action use mvn verify This will launch a Jetty instance deploy the webapp run the tests and then shut down the Jetty Or if you prefer to run the tests individually start and deploy with mvn jetty run and then run the tests as ordinary JUnit tests Now go ahead and browse the source code or get it as a zip I hope this post have helped to show you how simple it is to get support for both JSON and XML in web services using Spring MVC Posted by Daniel Sawano Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Labels Jackson Java JAXB JSON REST RESTful Spring Spring MVC XML 18 comments Anonymous 30 May 2012 08 51 Great post I spent my whole day struggling with this yesterday all I needed to do was to switch to Spring 3 1 and remove all the crap from the XML descriptor Thanks Julien Reply Delete Replies Daniel Sawano 30 May 2012 11 28 Thanks Glad you found it helpful Julien Delete Reply Anonymous 21 June 2012 08 04 Thank you very much Daniel This post helped me greatly Reply Delete Anonymous 06 July 2012 09 37 What if you need a list of users how would you do it Thanks Reply Delete Replies Daniel Sawano 13 July 2012 07 19 You would typically have to put it in an object containing the list of users Like a transfer object Delete Reply Hoser 10 July 2012 16 27 Awesome it felt good to rip out all that extra config that was not needed Reply Delete Replies Daniel Sawano 13 July 2012 07 23 Hi Hoser yes the configuration does gets a lot cleaner Delete Reply Anonymous 26 September 2012 07 51 Thank you for your post much appreciated Will help kick off a new project cleanly Reply Delete Anonymous 28 January 2013 11 17 I worked on this one its seems straight But somehow I test in jetty plugin for maven i get following response curl http localhost 8080 fxaip core server app ref currency currency INR HAccept application xml html head meta http equiv Content Type content text html charset ISO 8859 1 title Error 406 NOT ACCEPTABLE title head body h2 HTTP ERROR 406 h2 pre NOT ACCEPTABLE pre p RequestURI fxaip core server app ref currency currency INR p p i small a href http jetty mortbay org Powered by Jetty a small i p br br br br br br br br br br br br br br br br br br br br body html curl http localhost 8080 fxaip core server app ref currency currency INR HAccept application json value India Rupee id 0 code INR Reply Delete Replies Daniel Sawano 11 February 2013 06 18 Hi Both curl http localhost

    Original URL path: http://software.sawano.se/2012/03/combining-json-and-xml-in-restful-web.html (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Controlling message converters with Spring MVC 3.1
    simply a matter of defining our own order for the message converters and make sure the JSON converter comes before the Jaxb converter We can do this by using the new mvc message converters configuration element in Spring MVC 3 1 If we change our configuration from this mvc annotation driven to a configuration that specifies the order of the message converters explicitly like this mvc annotation driven mvc message converters bean class org springframework http converter json MappingJacksonHttpMessageConverter bean class org springframework http converter xml Jaxb2RootElementHttpMessageConverter mvc message converters mvc annotation driven we will get the desired result If you run the above integration test again you will now see that the data is returned in JSON format If you want to prevent any default message converters to be added in addition to the ones you explicitly specify then you can control that with the register default attribute An example of that would be mvc message converters register defaults false With the new features in Spring MVC 3 1 controlling message converters has become really easy and we have now enhanced our REST API to return JSON by default Posted by Daniel Sawano Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Labels Jackson Java JAXB JSON Spring Spring MVC XML 2 comments Anonymous 12 June 2012 04 14 Hi Daniel I read your previous and this post I tried to implement the similar thing but unfortunately it is always returning XML regardless of the message converters defined Any idea Reply Delete Replies Daniel Sawano 13 June 2012 12 57 Hi remember that you need to remove the previous line in the example Otherwise it will interfere Also it is all based on the accept header so if you are testing with a browser you need

    Original URL path: http://software.sawano.se/2012/03/controlling-message-converters-with.html?showComment=1339499647366 (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Controlling message converters with Spring MVC 3.1
    simply a matter of defining our own order for the message converters and make sure the JSON converter comes before the Jaxb converter We can do this by using the new mvc message converters configuration element in Spring MVC 3 1 If we change our configuration from this mvc annotation driven to a configuration that specifies the order of the message converters explicitly like this mvc annotation driven mvc message converters bean class org springframework http converter json MappingJacksonHttpMessageConverter bean class org springframework http converter xml Jaxb2RootElementHttpMessageConverter mvc message converters mvc annotation driven we will get the desired result If you run the above integration test again you will now see that the data is returned in JSON format If you want to prevent any default message converters to be added in addition to the ones you explicitly specify then you can control that with the register default attribute An example of that would be mvc message converters register defaults false With the new features in Spring MVC 3 1 controlling message converters has become really easy and we have now enhanced our REST API to return JSON by default Posted by Daniel Sawano Email This BlogThis Share to Twitter Share to Facebook Share to Pinterest Labels Jackson Java JAXB JSON Spring Spring MVC XML 2 comments Anonymous 12 June 2012 04 14 Hi Daniel I read your previous and this post I tried to implement the similar thing but unfortunately it is always returning XML regardless of the message converters defined Any idea Reply Delete Replies Daniel Sawano 13 June 2012 12 57 Hi remember that you need to remove the previous line in the example Otherwise it will interfere Also it is all based on the accept header so if you are testing with a browser you need

    Original URL path: http://software.sawano.se/2012/03/controlling-message-converters-with.html?showComment=1339617471450 (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Take your REST API to the next level with HATEOAS
    that data should be formatted so the server can understand it First off we need to provide some sort of template describing the data that can be POSTed and secondly we need a way to tell what template goes with what link The way we do this is by introducing the concept of commands We extend the links with a command attribute that specifies the name of the command that should be used with that link The command itself simply contains the template The link for entering a new order would look like this after applying a command to it rel add href http foo bar trading equities 123456 method POST command addOrder And the addOrder command will look like this commandName addOrder template Some template Now that we have the command structure in place we need to choose a format for the template that will describe the format of the POSTed data If our API was using HTML then this could be accomplished by returning a HTML form with all the required input fields etc However our API is using JSON so we cannot do that JSON Schema to the rescue By using a JSON Schema as our template we can describe the JSON data that should be POSTed and the client can use parse this schema on the fly and present a form to the user that allows her to input relevant fields The JSON Schema can for example also be used to describe what possible values that are valid for fields that have limited input options For example an order in our trading service can only be of one out of three different types Fill and Kill Fill or Kill and Good Til Cancelled The schema let us specify the possible values and the client could present these to the user as a drop down To put this in context here is how we would go about to enter a new order First we need to get to the instrument equity we want to trade in This is done by performing a search or viewing a list of all instruments From the search hit we get a link http foo bar trading equities 123456 that we can perform a GET on to get the details of the instrument For simplicity we assume that one instrument only have one orderbook that you can place an order in in reality there are several orderbooks but that is more domain detail than we need for this example The GET request would then return information about the instrument and a link to the URL for entering a new order instrument instrumentKey mic XNAS isin US0378331005 currency USD orderbookId 123456 longName Apple Inc shortName AAPL links rel self href http foo bar trading equities 123456 rel add href http foo bar trading equities 123456 method POST command addOrder commands commandName addOrder template type object properties size type number openSize type number price type number marketPriceOrder type boolean side type string enum BUY

    Original URL path: http://software.sawano.se/2012/04/take-your-rest-api-to-next-level-with.html?showComment=1333549582351 (2016-05-02)
    Open archived version from archive

  • sôft'wâr': Take your REST API to the next level with HATEOAS
    that data should be formatted so the server can understand it First off we need to provide some sort of template describing the data that can be POSTed and secondly we need a way to tell what template goes with what link The way we do this is by introducing the concept of commands We extend the links with a command attribute that specifies the name of the command that should be used with that link The command itself simply contains the template The link for entering a new order would look like this after applying a command to it rel add href http foo bar trading equities 123456 method POST command addOrder And the addOrder command will look like this commandName addOrder template Some template Now that we have the command structure in place we need to choose a format for the template that will describe the format of the POSTed data If our API was using HTML then this could be accomplished by returning a HTML form with all the required input fields etc However our API is using JSON so we cannot do that JSON Schema to the rescue By using a JSON Schema as our template we can describe the JSON data that should be POSTed and the client can use parse this schema on the fly and present a form to the user that allows her to input relevant fields The JSON Schema can for example also be used to describe what possible values that are valid for fields that have limited input options For example an order in our trading service can only be of one out of three different types Fill and Kill Fill or Kill and Good Til Cancelled The schema let us specify the possible values and the client could present these to the user as a drop down To put this in context here is how we would go about to enter a new order First we need to get to the instrument equity we want to trade in This is done by performing a search or viewing a list of all instruments From the search hit we get a link http foo bar trading equities 123456 that we can perform a GET on to get the details of the instrument For simplicity we assume that one instrument only have one orderbook that you can place an order in in reality there are several orderbooks but that is more domain detail than we need for this example The GET request would then return information about the instrument and a link to the URL for entering a new order instrument instrumentKey mic XNAS isin US0378331005 currency USD orderbookId 123456 longName Apple Inc shortName AAPL links rel self href http foo bar trading equities 123456 rel add href http foo bar trading equities 123456 method POST command addOrder commands commandName addOrder template type object properties size type number openSize type number price type number marketPriceOrder type boolean side type string enum BUY

    Original URL path: http://software.sawano.se/2012/04/take-your-rest-api-to-next-level-with.html?showComment=1333565184857 (2016-05-02)
    Open archived version from archive