in realtà ho capito che si trattava di una linea nel resto del plugin causando questo:
// don't return any content for PUT, DELETE, and POST where there are no errors
if (!hasErrors && !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
target = null;
}
Questo è in org.apache.struts2.rest.RestActionInvocation
nel metodo selectTarget()
. Trovo che questo sia piuttosto fastidioso in quanto in realtà non segue l'architettura REST, id come l'opzione di essere in grado di restituire gli oggetti risposta per le richieste POST, DELETE e PUT in alcuni casi.
ho lavorato intorno a questo estendendo RestActionProxyFactory
e RestActionInvocation
e specificando l'uso di questo nel mio montanti xml in questo modo:
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="restOverride" class="uk.co.ratedpeople.tp.rest.RPRestActionProxyFactory" />
<constant name="struts.actionProxyFactory" value="restOverride" />
Questo mi permette di utilizzare i montanti plug tutto mentre tornava oggetto richieste POST.
RestActionProxyFactory
public class RPRestActionProxyFactory extends RestActionProxyFactory {
@Override
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map extraContext, boolean executeResult, boolean cleanupContext) {
if (namespace.startsWith(this.namespace)) {
ActionInvocation inv = new RPRestActionInvocation(extraContext, true);
container.inject(inv);
return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
} else {
return super.createActionProxy(namespace, actionName, methodName, extraContext, executeResult, cleanupContext);
}
}
}
RestActionInvocation
public class RPRestActionInvocation extends RestActionInvocation {
public RPRestActionInvocation(Map extraContext, boolean pushAction) {
super(extraContext, pushAction);
}
@SuppressWarnings("unchecked")
@Override
protected void selectTarget() {
// Select target (content to return)
Throwable e = (Throwable)stack.findValue("exception");
if (e != null) {
// Exception
target = e;
hasErrors = true;
} else if (action instanceof ValidationAware && ((ValidationAware)action).hasErrors()) {
// Error messages
ValidationAware validationAwareAction = ((ValidationAware)action);
Map errors = new HashMap();
if (validationAwareAction.getActionErrors().size() > 0) {
errors.put("actionErrors", validationAwareAction.getActionErrors());
}
if (validationAwareAction.getFieldErrors().size() > 0) {
errors.put("fieldErrors", validationAwareAction.getFieldErrors());
}
target = errors;
hasErrors = true;
} else if (action instanceof ModelDriven) {
// Model
target = ((ModelDriven)action).getModel();
} else {
target = action;
}
// don't return any content for PUT, DELETE, and POST where there are no errors
// if (!hasErrors && !"get".equalsIgnoreCase(ServletActionContext.getRequest().getMethod())) {
// target = null;
// }
}
}
non riuscivo a trovare un modo per aggirare questo, ma ho optato per alterare il codice di risposta restituendo DefaultHttpHeaders invece di SUCCESSO. Penso che questo potrebbe essere il modo di andare con le richieste POST e PUT. – noShowP
Ciao, sto usando anche il plugin struts2-rest-plugin e sto avendo problemi ad ottenere il metodo update() per accettare un payload JSON e mapparlo correttamente al modello. Ti dispiace condividere come hai fatto? Grazie .. – shaunlim