Struts2.5 JSONInterceptor is not populating the Ac

2020-03-30 05:35发布

问题:

I am building a CRUD application using embers as client and struts2 as server. The post request to create a record from ember looks like,

Accept: application/json, text/javascript, /; q=0.01 Accept-Encoding: gzip, deflate, br Accept-Language: en-GB,en-US;q=0.9,en;q=0.8 Connection: keep-alive Content-Length: 54 Content-Type: application/json; charset=UTF-8 Host: localhost:8080 Origin: http://localhost:4200 Referer: http://localhost:4200/create Sec-Fetch-Mode: cors Sec-Fetch-Site: same-site User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36 {product: "{"name":"iamironman","price":3000}"} product: "{"name":"iamironman","price":3000}"

ember request payload

Struts.xml :

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="default"/>
    <constant name="struts.convention.package.locators" value="controller"/>
    <constant name="struts.rest.content.restrictToGET" value="false"/>

   <package name="default" namespace="/" extends="rest-default,json-default">
        <interceptors>
            <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
            <interceptor-stack name="myStack">
                <interceptor-ref name="json">
                    <param name="jsonContentType">application/json</param>
                </interceptor-ref>
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="myStack" />
        <global-allowed-methods>index,show,create,update,destroy</global-allowed-methods>
        <action name="products/*" class="controller.ProductsController">
            <interceptor-ref name="myStack" />
            <result name="index" type="json"></result>
        </action>
   </package>
</struts>

Action class :

package controller;
import java.util.List;
//import javax.servlet.http.HttpServletRequest;
import dao.Productdao;
import model.product;
//import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import com.opensymphony.xwork2.ModelDriven;

//@InterceptorRef(value="json")
public class ProductsController implements ModelDriven<Object>{
    //private static final Logger log = LogManager.getLogger(ProductsController.class);
    product p=new product();
    Productdao dao=new Productdao();
    private List<product> productlist=dao.getAllProduct();
    private int id;
    private String name;
    private double price;
    //private HttpServletRequest request;
    //private JSONObject Object;

    public HttpHeaders show()
    {
        System.out.println("Showing");
        p=dao.getProduct(id);
        return new DefaultHttpHeaders("show");
    }

    public HttpHeaders index() {
        System.out.println("i am inside index");
        return new DefaultHttpHeaders("index").disableCaching();
    }

    public String create()
    {
        System.out.println("order creation : "+name+price);
        boolean result=dao.createProduct(name,price);
        if(result)
        {
            System.out.println("product created successfully");
            return "success";
        }
        else
        {
            System.out.println("product Not created");
            return "increated";
        }
    }

    public String update()
    {
        System.out.println("inside update"+id+name+price);
        p.setId(id);
        p.setName(name);
        p.setPrice(price);
        boolean result=dao.updateProduct(p);
        if(result)
        {
            System.out.println("product updated successfully");
            return "success";
        }
        else
        {
            System.out.println("product not updated");
            return "inupdated";
        }
    }
    public String destroy()
    {
        System.out.println("inside destroy"+id);
        boolean result=dao.deleteProduct(id);
        if(result) {
            System.out.println("deleted successfully");
            return "success";
        }
        else
        {
            System.out.println("Not deleted");
            return "indeleted";
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    /*public JSONObject getObject() {
        return Object;
    }

    public void setObject(JSONObject object) {
        Object = object;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }*/

    @Override
    public Object getModel() {
        System.out.println("getting model");
        return productlist;
    }

}

Error :

java.sql.SQLIntegrityConstraintViolationException: Column 'name' cannot be null
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
    at dao.Productdao.createProduct(Productdao.java:40)
    at controller.ProductsController.create(ProductsController.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at ognl.OgnlRuntime.invokeMethodInsideSandbox(OgnlRuntime.java:1226)
    at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:1211)
    at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1920)
    at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:98)
    at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:90)
    at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1996)
    at ognl.ASTMethod.getValueBody(ASTMethod.java:91)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    at ognl.SimpleNode.getValue(SimpleNode.java:258)
    at ognl.Ognl.getValue(Ognl.java:537)
    at ognl.Ognl.getValue(Ognl.java:501)
    at com.opensymphony.xwork2.ognl.OgnlUtil$3.execute(OgnlUtil.java:492)
    at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecuteMethod(OgnlUtil.java:544)
    at com.opensymphony.xwork2.ognl.OgnlUtil.callMethod(OgnlUtil.java:490)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:438)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:293)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:250)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:179)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:49)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.doIntercept(ConversionErrorInterceptor.java:142)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:137)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:137)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:201)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:67)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:133)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:101)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:142)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:160)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:175)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:121)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:167)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:203)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:196)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at org.apache.struts2.json.JSONInterceptor.intercept(JSONInterceptor.java:185)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:130)
    at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:157)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:574)
    at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:79)
    at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:141)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.filters.CorsFilter.handleSimpleCORS(CorsFilter.java:259)
    at org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:163)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

What am I doing wrong. Thanks in advance..!

回答1:

it turns out that I just have to serialise my son in client side while sending.

serializeIntoHash(hash, type, record, options) { Ember.assign(hash, this.serialize(record, options)); }

which turned the son

{product: { "name": "iaminonman", "price": 3000.0} }

to

{"name":"iaminonman", "price":3000.0}