gpt4 book ai didi

java - Struts2 Rest Mapper 接管非 Rest Json 结果

转载 作者:行者123 更新时间:2023-11-30 07:08:40 31 4
gpt4 key购买 nike

Struts 版本:2.5.2

POM 中的 Struts 依赖关系

 <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${org.strutsframework-version}</version>
</dependency>


<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${org.strutsframework-version}</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-rest-plugin</artifactId>
<version>${org.strutsframework-version}</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${org.strutsframework-version}</version>
</dependency>

Struts XML

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- Tell jinjava where the templates are -->
<constant name="struts.jinjava.basepath" value="WEB-INF/jinjava" />
<!-- custom jinjava tags specific to iws -->
<constant name="struts.jinjava.scan.tagPackage" value="com.hs.iws.jinjava.tag" />
<constant name="struts.jinjava.scan.functionPackage" value="com.hs.iws.jinjava.function" />

<!--Tell struts to use the REST action Mapper-->
<!--<constant name="struts.mapper.class" value="rest"/>-->

<!-- allow rest and non rest actions to live together -->
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value=":rest,/grid:struts"/>
<constant name="struts.rest.namespace" value="/" />

<constant name="struts.convention.action.suffix" value="Action"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.package.locators.basePackage" value="com.hs.iws.actions" />

<!--re-assert the extensions for struts that have been over written by the rest plugin-->
<constant name="struts.action.extension" value="xhtml,,json,action"/>
<constant name="struts.rest.content.restrictToGET" value="false" />

<!--configure Convention Plugin to find our controllers-->
<constant name="struts.convention.default.parent.package" value="iws-default"/>

<!-- Spring Configuration -->
<!-- <constant name="struts.objectFactory" value="spring" /> -->
<constant name="struts.objectFactory.spring.autoWire" value="type" />


<!-- all grid actions should fall under this package -->
<package name="iws-grid" namespace="/grid" extends="struts-default,jweb-struts-gson-json,jinjava,datatables">
<interceptors>

<interceptor-stack name="iws-datatable-stack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="gson-json" />
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
</interceptor-stack>

</interceptors>

<default-interceptor-ref name="iws-datatable-stack" />
</package>


<package name="iws-default" extends="rest-default, struts-default, jinjava, jweb-struts-gson-json" namespace="/">


</package>
</struts>

Action 类

package com.hs.iws.actions;

import com.hs.datatables.DataTable10CriteriaQuery;
import com.hs.datatables.DataTable10Helper;
import com.hs.iws.model.Users;
import org.apache.struts2.convention.annotation.*;

/**
* Created by Paul on 9/14/2016.
*/
@InterceptorRef(value = "iws-datatable-stack")
@ParentPackage(value = "iws-grid")
public class TestGridAction extends DataTable10CriteriaQuery{

@Action(value="/test-grid-json",
results={
@Result(name = "success", type = "datatable")
}
)
public String execute() {
return super.execute();
}

@Override
protected Class<?> getHibernateClass() {
return Users.class;
}
}

我正在使用 DataTables JS 库,并尝试为网格编写操作。我已经有一个 api 可以为我创建 json,我只需要将其流式传输回来即可。我创建了一个自定义结果来处理此问题,但操作中映射的结果从未运行。无论结果类型是什么,REST 映射器在看到客户端请求了 application/json 后都会尝试处理它。我在配置中使用了前缀映射,让所有 url 使用/grid 来绕过其余映射器。它似乎在以某种方式工作,因为它运行正确的拦截器堆栈并使用 @Action 注释信息来映射 url。但是,指定的结果并未运行,而是由其余映射器根据我收到的堆栈跟踪提供。我想完全绕过/grid 命名空间中的任何操作的其余映射器。我在配置中是否做错了什么,仍然导致其余操作参与到对这些操作的请求中?

堆栈跟踪

ERROR RestActionInvocation Exception processing the result.
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject._fromBean(JSONObject.java:987)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
at net.sf.json.JSONArray._processValue(JSONArray.java:2514)
at net.sf.json.JSONArray.processValue(JSONArray.java:2539)
at net.sf.json.JSONArray.addValue(JSONArray.java:2526)
at net.sf.json.JSONArray._fromCollection(JSONArray.java:1057)
at net.sf.json.JSONArray.fromObject(JSONArray.java:123)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:237)
at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.JSONObject.fromObject(JSONObject.java:130)
at org.apache.struts2.rest.handler.JsonLibHandler.fromObject(JsonLibHandler.java:72)
at org.apache.struts2.rest.DefaultContentTypeHandlerManager.handleResult(DefaultContentTypeHandlerManager.java:181)
at org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:227)
at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:194)
at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:142)
at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:154)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:556)
at org.apache.struts2.dispatcher.ExecuteOperations.executeAction(ExecuteOperations.java:81)
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:113)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at com.hs.security.SecurityScanner.doFilter(SecurityScanner.java:95)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:105)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1078)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:760)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1524)
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)
Caused by: java.lang.reflect.InvocationTargetException
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:497)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2116)
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1267)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:808)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:884)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464)
at net.sf.json.JSONObject._fromBean(JSONObject.java:918)
... 52 more
Caused by: java.lang.UnsupportedOperationException: JsonObject
at com.google.gson.JsonElement.getAsByte(JsonElement.java:257)
... 62 more

最佳答案

我也遇到过类似的问题。首先,您可能已经知道 struts2-rest-plugin 不仅可以返回 json,还可以返回 xml 和 xhtml(通过打开 url 中的文件扩展名)。结果不起作用的恼人原因是 strtus2-rest-plugin 不使用结果,而是使用 ContentTypeHandlers,它取代任何使用结果的尝试。

我认为在你的情况下,.action 后缀会破坏其余插件,因此它会寻找另一个匹配的操作,可能使用普通的旧约定。

在我自己的代码中,我创建了一个restful 和non-restful 包来回避这个问题。我还需要创建自己的内容类型处理程序来替换它们的默认处理程序。可以将自定义结果转换为自定义内容类型处理程序,但除非它是“xml、json或xhtml”类型,否则我认为创建一个非静态包,并使用它来容纳这些操作更有意义.

如果今晚有时间,我将提供一份用于创建两组包的 struts.xml 副本。这将很有用,因为我发现 struts2-rest-plugin 的配置很脆弱(不是很直观,并且需要对我来说不完全有意义的行,这些行是通过猜测而不是逻辑添加的)。

<?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" />
<!-- the next two lines are ONLY if you want to override a content handler, and since mine is custom it would be with your own impl, however without pain you can only overrride because I think think the extensions are hard coded... so you can't just add your own, could be wrong -->
<bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="flexjson" class="com.kenmcwilliams.s2.result.FlexJsonHandler" />
<constant name="struts.rest.handlerOverride.json" value="flexjson"/>

<constant name="struts.action.extension" value="xhtml,,xml,json,action"/>
<constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.PrefixBasedActionMapper" />
<constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="my-conventions"/>
<constant name="struts.rest.namespace" value="/rest"/>

<package name="my-conventions" namespace="/" extends="convention-default" >
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<!-- Following is required for some reason -->
<global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index,show,create,update,destroy,edit,editNew</global-allowed-methods>
</package>

<package name="my-rest" namespace="/rest" extends="rest-default">
<result-types>
<result-type name="flexjson" class="com.kenmcwilliams.s2.result.FlexJsonResult"/>
</result-types>
</package>

<!-- not needed unless you're planning on using tiles -->
<package name="my-tiles" namespace="/tiles" extends="tiles-default" strict-method-invocation="false">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
</package>
</struts>

关于java - Struts2 Rest Mapper 接管非 Rest Json 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39593721/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com