Thursday, January 3, 2013

Spring MVC and NetBeans Rest service from database tutorial: 415 Unsupported Media Type



I was doing the Spring MVC and NetBeans Rest service from database tutorial

http://netbeans.org/kb/docs/websvc/rest.html#rest-spring

But now wanted to consume the service with an ajax request in jquery:

                             var data = {"id":"1","identification":"1010182323","name":"jhon"};

                            $.ajax({                                           
                                                url: "/notes/"
                                                ,type: 'POST'
                                                ,dataType: "json"                                        
                                                ,data: data                                               
                                                ,success: function(data, textStatus, jqXHR){                                                  
                                                },
                                                error: function(jqXHR, textStatus, errorThrown){                                                
                                                }
                                            });


And I was getting the error:

 415 Unsupported Media Type

The solution was specifying the contentType and encoding the data to json:

                                     var data = {"id":"1","identification":"1010182323","name":"jhon"};

                                     $.ajax({                                          
                                                url: "/notes/"
                                                ,type: 'POST'
                                                ,dataType: "json"                                                
                                                ,data: Ext.JSON.encode(data) // here
                                                ,contentType: "application/json" // here                                                
                                                ,success: function(data, textStatus, jqXHR){
                                                },
                                                error: function(jqXHR, textStatus, errorThrown){
                                                }
                                            });

Here is an example of how to use the Ext.JSON.encode method

http://jhonjairoroa87.blogspot.com/2012/10/javascripy-simple-encodedecode-json.html

Resources:
http://docs.oracle.com/cd/E19226-01/820-7627/giepu/index.html
http://www.checkupdown.com/status/E415_es.html
http://stackoverflow.com/questions/9754767/cannot-set-content-type-to-application-json-in-jquery-ajax
http://api.jquery.com/jQuery.ajax/


Javascript: clone array

To clone an array in javascript you can use the slice() method:


var a = [ 'apple', 'orange', 'grape' ];
var b = [];

b = a.slice(0);
b[0] = 'cola';

console.log(a);//[ 'apple', 'orange', 'grape' ]
console.log(b);//[ 'cola', 'orange', 'grape' ]


Resource:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice
http://www.xenoveritas.org/blog/xeno/the-correct-way-to-clone-javascript-arrays

Spring MVC and NetBeans Rest service from database tutorial: many to many relationship


I was doing the Spring MVC and NetBeans Rest service from database tutorial

http://netbeans.org/kb/docs/websvc/rest.html#rest-spring

But now I applyied the tutorial to do the REST services to this database:


after finishing all the tutorial applied to the previous database, I test the URL

http://localhost:8080/WebApplication1/webresources/entities.incidents/1

and I got

{"datetime":"2012-12-28T00:00:00-05:00","description":"jIMAExx","id":"1","user":{"id":"2","identification":"1010123223","name":"pedros"}}

But now I wanted to get the categories corresponding to the many to many relationship, so I changed the incidents.java file from:
...
...
...
@XmlTransient
    public Collection<Categories> getCategoriesCollection() {
        return categoriesCollection;
    }

    public void setCategoriesCollection(Collection<Categories> categoriesCollection) {
        this.categoriesCollection = categoriesCollection;
    }
...
...
...
to
...
...
...
// @XmlTransient // Here, I just commented this line
    public Collection<Categories> getCategoriesCollection() {
        return categoriesCollection;
    }

    public void setCategoriesCollection(Collection<Categories> categoriesCollection) {
        this.categoriesCollection = categoriesCollection;
    }
...
...
...
Note that the only thing I did was commenting the @XmlTransient line. Then if I test  the url again I get:

{"categoriesCollection":[{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"}],"datetime":"2012-12-28T00:00:00-05:00","description":"jIMAExx","id":"1","user":{"id":"2","identification":"1010123223","name":"pedros"}}

Wednesday, January 2, 2013

Spring MVC and NetBeans Rest service from database tutorial error: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

I was doing the Spring MVC and NetBeans Rest service from database tutorial

http://netbeans.org/kb/docs/websvc/rest.html#rest-spring

When I finished all the steps and tryied to run the project and I got:

nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

the detailed log is:

 Error durante el despliegue: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor. Consulte server.log para obtener más información.
C:\Users\jroa\Documents\NetBeansProjects\WebApplication1\nbproject\build-impl.xml:1032: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 37 seconds)

I fixed it by deleting, from the project library, the aopalliance jar downloaded from http://sourceforge.net/projects/aopalliance/files/aopalliance/1.0/.

and adding, to the project library, the aopalliance jar downloaded from the spring web site
http://ebr.springsource.com/repository/app/bundle/version/detail?name=com.springsource.org.aopalliance&version=1.0.0