1. #1
    Sencha User
    Join Date
    Apr 2013
    Posts
    1
    Vote Rating
    0
    david.c.wang@jpmchase.com is on a distinguished road

      0  

    Default Having problem making JSON POST call to Apache CXF Service

    Having problem making JSON POST call to Apache CXF Service


    Hi everyone,

    I am having trouble to get JSON POST call working. Here's my setup, I am running apache CXF in service in the backend which consumes and produces JSON. On the frontend, I am using Ext JS 4 MVC to call the service. I was able to make the GET, but when trying to make POST call, I am getting "415 Unsupported Media Type" error. I have been searching around to see if there's a good example for making POST to Apache CXF service. But I don't have much luck. I hope someone call help me out here. Here's my code:
    Backend setup:
    Code:
    @Path("/contactservice")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public class ContactService {
        // spring injected
        ContactManager contactManager;
    
        @GET
        @Path("/getall")
        @Produces(MediaType.APPLICATION_JSON)
        public Contacts getContacts(@QueryParam("start") int start, @QueryParam("limit") int limit) {
            return contactManager.retrieveContacts(start, limit);
        }
    
        @POST
        @Path("/save")
        @Consumes(MediaType.APPLICATION_JSON)
        public void saveContact(Contact contact) {
    
            // save action
        }
    
        public void setContactManager(ContactManager contactManager) {
            this.contactManager = contactManager;
        }
    }
    ApplicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:jaxrs="http://cxf.apache.org/jaxrs" 
        xmlns:cxf="http://cxf.apache.org/core" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                  http://www.springframework.org/schema/beans/spring-beans.xsd 
                  http://cxf.apache.org/jaxrs 
                  http://cxf.apache.org/schemas/jaxrs.xsd 
                  http://cxf.apache.org/core 
                  http://cxf.apache.org/schemas/core.xsd"> 
    
        <import resource="classpath:META-INF/cxf/cxf.xml"/> 
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
        <jaxrs:server id="contactService" address="/contact"> 
            <jaxrs:serviceBeans> 
                <ref bean="contactServiceBean"/> 
            </jaxrs:serviceBeans> 
            <jaxrs:extensionMappings> 
                <entry key="xml" value="application/xml" /> 
                <entry key="json" value="application/json" /> 
            </jaxrs:extensionMappings> 
            <jaxrs:providers>
                <ref bean="cors-filter" />
                <ref bean="jacksonJsonProvider" />
            </jaxrs:providers> 
            <jaxrs:features> 
                <cxf:logging/> 
            </jaxrs:features> 
        </jaxrs:server> 
        <bean id="contactServiceBean" class="com.myservice.ContactService">
            <property name="contactManager" ref="contactManager"/>     
        </bean>
        <bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"/>
        <bean id="jacksonJsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> 
    </beans>
    Ext JS Code:
    ContactController.js
    Code:
    Ext.define('ExtMVC.controller.ContactController', {
        extend: 'Ext.app.Controller',
    
        stores: ['Contacts'],
    
        models: ['Contact'],
    
        views: ['contact.ContactForm', 'contact.Grid'],
    
        refs: [{
                ref: 'contactPanel',
                selector: 'panel'
            },{
                ref: 'contactGrid',
                selector: 'grid'
            }
        ],
    
        init: function() {
            this.control({
                'contactgrid dataview': {
                    itemdblclick: this.editContact
                },
                'contactgrid button[action=add]': {
                    click: this.addContact
                },
                'contactgrid button[action=delete]': {
                    click: this.deleteContact
                },
                'contactform button[action=save]': {
                    click: this.updateContact
                }
            });
        },
    
        addContact: function() {
            //console.log('addContact()');
            Ext.create('ExtMVC.view.contact.ContactForm').show();
        },
    
        editContact: function(grid, record) {
            //console.log('editContact()');
            var edit = Ext.create('ExtMVC.view.contact.ContactForm').show();
            if(record){
                //console.log(edit.down('form'));
                edit.down('form').loadRecord(record);
            }
        },
        
        updateContact: function(button) {
            //console.log('updateContact()');
            var win    = button.up('window'),
                form   = win.down('form'),
                record = form.getRecord(),
                values = form.getValues();
            //console.log('save');
            var newRecord = false;
            
            if (values.id > 0){
                record.set(values);
            } else{
                record = Ext.create('ExtMVC.model.Contact');
                record.set(values);
                this.getContactsStore().add(record);
                newRecord = true;
            }
            
            win.close();
            this.getContactsStore().sync();
    
            if (newRecord){ //faz reload para atualziar
                this.getContactsStore().load();
            }
        },
        
        deleteContact: function(button) {
            
            var grid = this.getContactGrid(),
            record = grid.getSelectionModel().getSelection(), 
            store = this.getContactsStore();
    
            store.remove(record);
            this.getContactsStore().sync();
    
            //faz reload para atualziar
            this.getContactsStore().load();
        }
    });
    Store:
    Code:
    Ext.define('ExtMVC.store.Contacts', {
        extend: 'Ext.data.Store',
        model: 'ExtMVC.model.Contact',
        autoLoad: true,
        pageSize: 35,
    
        listeners: {
            beforeload: function(store, operation, opetions) {
                //console.log("loading...");
            }
        },
        
        proxy: {
            type: 'ajax',
            actionMethods: {
                create: 'POST',
                read: 'GET',
                update: 'POST',
                destroy: 'POST'
            },
            api: {
                create: 'http://localhost:8080/dataportal/contact/contactservice/save',
                read: 'http://localhost:8080/dataportal/contact/contactservice/getall'
            },
            reader: {
                type: 'json',
                root: 'contacts',
                successProperty: 'success'
            },
            writer: {
                type: 'json',
                writeAllFields: true,
                encode: true,
                root: 'contact'
            }
        }
    });

  2. #2
    Sencha Premium Member
    Join Date
    May 2010
    Location
    Guatemala, Central America
    Posts
    1,267
    Vote Rating
    81
    ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough ssamayoa is a jewel in the rough

      0  

    Default


    It seems that CFX simply can't deserialize the JSON object sent by ExtJS application or is picky about content type sent.

    Check what is sending ExtJS to your rest code (firebug or Chrome's "inspect...").

    BTW, REST verbs for UPDATE and DESTROY should be PUT and DELETE.

    Regards.

    PD: Why you are bothering with CFX? Use a JEE 5/6 full container like Glassfish or JBoss.
    UI: Sencha Architect 3.x / ExtJS 4 & 5
    Server side: JEE / EJB 3.x / CDI / JPA 2.x/ JAX-RS / JasperReports
    Application Server: Glassfish / WildFly
    Databases: Oracle / DB2 / MySQL / Firebird

    If you like my answer please vote!

Thread Participants: 1

Tags for this Thread