0

Im using Struts2-Rest-Plugin v2.3.1.2. I need to write a POST REST method, that will receive JSON object as request and return JSON object as Response. The JSON fields are passed as null to the Controller create() method.

Can you help me why the JSON fields are passed as NULL?

struts.xml:

<constant name="struts.action.extension" value="xhtml,,xml,json,action" />
<constant name="struts.rest.content.restrictToGET" value="false" />
<constant name="struts.convention.action.suffix" value="Action, Controller" />
<constant name="struts.convention.action.mapAllMatches" value="true" />
<constant name="struts.convention.default.parent.package" value="rest-default" />
<constant name="struts.convention.package.locators" value="rest" />
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value="/api:rest,:struts" />
<constant name="struts.rest.namespace" value="/api" />
<constant name="struts.rest.defaultExtension" value="json" />


<package name="api" namespace="/api" extends="rest-default">
     <action name="registrations/*" class="com.sample.rest.RegistrationsController">
    </action> 
</package> 

RegistrationsController.Java:

package com.sample.rest;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.sample.rest.payload.RegistrationRequest;
import com.sample.rest.payload.RegistrationResponse;
import com.sample.service.RegistrationService;
import com.opensymphony.xwork2.ModelDriven;

public class RegistrationsController  implements ModelDriven<Object>{

    private RegistrationRequest request = new RegistrationRequest();
    private RegistrationResponse response = new RegistrationResponse();
    RegistrationService registerService = new RegistrationService();
    private String requestId;

    private Object model;


    // POST /api/registrations
        public HttpHeaders create() {
            response = registerService.registerProject(request);
            System.out.println("request : " + request.toString());
            model = response;
            System.out.println("POST \t /registrations" + request.getProjectDetails().getProjectId());
            return new DefaultHttpHeaders("create");
        }

        /**
         * 
         */
        public RegistrationsController() {
            super();
        }



    @Override
    public Object getModel() {
        return model;
    }

    /**
     * @return the registerService
     */
    public RegistrationService getRegisterService() {
        return registerService;
    }

    /**
     * @param registerService the registerService to set
     */
    public void setRegisterService(RegistrationService registerService) {
        this.registerService = registerService;
    }

    /**
     * @return the request
     */
    public RegistrationRequest getRequest() {
        return request;
    }

    /**
     * @param request the request to set
     */
    public void setRequest(RegistrationRequest request) {
        this.request = request;
    }

    /**
     * @return the requestId
     */
    public String getRequestId() {
        return requestId;
    }

    /**
     * @param requestId the requestId to set
     */
    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

}

Result from Console:

request : RegistrationRequest [requestId=null, clientId=null, timestamp=null, projectDetails=null]

Thanks and Regards Sam

  • HI Roman,The link you have specified for reference uses primitive datatypes as fields in JSON body. In my case, I have to pass an POJO as request object. – user1256795 Dec 26 '16 at 04:20
  • HI Roman,The link you have specified for reference uses primitive datatypes as fields in JSON body. In my case, I have to pass an POJO as request object. Is there any other setting i Have to do in struts.xml for this? – user1256795 Dec 26 '16 at 04:21

0 Answers0