Saturday, March 28, 2015

Spring 3 MVC and REST

For anyone working with Spring 3 MVC and REST, I spent a lot of time getting this sorted, so I thought I would post some results.

Spring 3 is a few years old now, but I'm using it at my day job, so that's what I'm using here. You can get a Spring 3 MVC project running by following any number of tutorials. I won't go into those details here.

The biggest gotcha I ran into wasn't well documented anywhere, in fact, I found two singular pieces of information which enabled me to get this code working after some considerable effort. First is using Jackson version 1.9, instead of 2.x, for Spring MVC versions prior to 3.1. Here's the POM entries.

<properties>
 <jackson.version>1.9.13</jackson.version>
</properties>

<dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-core-asl</artifactId>
 <version>${jackson.version}</version>
</dependency>

<dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-mapper-asl</artifactId>
 <version>${jackson.version}</version>
</dependency>

Secondly, with Spring MVC 3.0, you need this in your bean configuration;

 <!-- JSON support -->
 <bean id="jacksonMessageConverter"
  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
 <bean
  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
   <list>
    <ref bean="jacksonMessageConverter" />
   </list>
  </property>
 </bean>

So, let's take a request from the beginning. I used Advanced Rest Client chrome extension, but this works as a simple URL request. Your web.xml file needs handle the request,

<!-- Initialise the Spring MVC DispatcherServlet -->
<servlet>
 <servlet-name>spring-myapp</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map the Spring MVC DispatcherServlet to intercept *.sp requests -->
<servlet-mapping>
 <servlet-name>spring-myapp</servlet-name>
 <url-pattern>*.sp</url-pattern>
</servlet-mapping> 

<servlet-mapping>
 <servlet-name>spring-myapp</servlet-name>
 <url-pattern>json/*</url-pattern>
</servlet-mapping> 


Spring maps the URL to a controller,

<context:component-scan base-package="com.nml.myapp.web.controller" />
<context:annotation-config />
<mvc:annotation-driven />

and with Jackson in the classpath, the controller code returns the JSON response,


@Controller
public class JsonController {

 private static final Logger logger = Logger.getLogger(JsonController.class);

 @RequestMapping(value = "/getCategories", method = RequestMethod.GET)
 public @ResponseBody List<String> query() {

  logger.info("rest request: getCategories");
  
  List<String> list = new ArrayList<String>();
  list.add("object is list");
  list.add("second object"); 
//  model.addAttribute("list", list);
  
  return list;
 }
}

Here's the result. My first good night's sleep in a week.


Chromebook and Crouton Goodness

I've purchased a new Chromebook, an Asus C300 EDU with a 13" screen, 4G RAM, 32G SSD, an Intel N2830 dual core processor. It's quite an improvement over the Samsung 303C that I've used for a couple of years, in fact, we have one for every member of the family. It's just easier that way!

One of the reasons I went with the Chromebook was the ease of use. Everything works with a Google Account, and it integrates well with Android. I use Google Docs extensively, so there's no need for lot's of disk space.

I got this particular model with an Intel processor for two reasons. 4G RAM is a necessity for intensive applications, and I use Crouton which installs Ubuntu Linux side by side with ChromeOS, which runs on a scaled down version of Linux. Some Linux applications depend on native compiled libraries which aren't available for ARM based processors, like the Samsung chromebooks and Raspberry Pi.

I've been using some Amateur Radio applications just fine on the ARM processor, but Eclipse wouldn't run, and I blogged earlier about hp printing, although cloud printing works fine.

So, now I've got this great Chromebook with plenty of memory, runs all my linux apps, and I've got it connected to an older Dell 24" external monitor, keyboard, and mouse. Life is good, praise the Lord! Amen and amen!

Sunday, May 18, 2014

CHIRP and Ubuntu 12.4

Why not? I've got a Chromebook running Linux, a programming cable for the Baofeng UV-B5 HT, and all I needed was the program, which just happens to be open source software recommended on Linux.

After installing from apt-get, the version was so old, what was the point of ever putting it there. Maybe more recent repos have newer versions. Anyway, d/l'd the latest tar, extracted it and ran it, although I was getting an error,

"could not open port /dev/ttyUSB0: [Errno 13] Permission denied:"

and the documentation I could find was either run the program as root [wrong], or add the user to the dialup group. This was partially right, the correct group was the serial group, and all is well.

Now I have a Baofeng programmed with all the repeaters in the area, around 62 of them. The CHIRP program allows imports and exports from CSV files, and downloads and uploads to and from the radio. I may have used CHIRP previously on Windows on a P25 GRE scanner I used to own, but it's always nice not to use Window$.