I'm writing a REST server, using struts2-rest-plugin. I'm testing the REST calls with SoapUI and Postman. I'm following the sample code here:
https://cwiki.apache.org/confluence/display/WW/REST+Plugin
My REST calls for controller.index()
)and controllers.create()
are working OK.
But although controller.update()
successfully calls the controller and updates the record:
19:46:05.361 [http-nio-8080-exec-3] DEBUG com.opensymphony.xwork2.validator.ValidationInterceptor - Validating /contacts with method update.
19:46:05.393 [http-nio-8080-exec-3] DEBUG com.opensymphony.xwork2.DefaultActionInvocation - Executing action method = update
19:46:05.398 [http-nio-8080-exec-3] DEBUG com.opensymphony.xwork2.ognl.SecurityMemberAccess - Checking access for [target: com.example.contactsapp.controllers.ContactsController@7e69160c, member: public java.lang.String com.example.contactsapp.controllers.ContactsController.update(), property: null]
19:46:07.862 [http-nio-8080-exec-3] DEBUG com.example.contactsapp.controllers.ContactsController - Updating existing contact(97)...
...
... it fails on "return", with this error:
...
19:46:11.380 [http-nio-8080-exec-3] WARN org.apache.struts2.dispatcher.Dispatcher - Could not find action or result: /StrutsContactsApp/contacts/97
com.opensymphony.xwork2.config.ConfigurationException: No result defined for action com.example.contactsapp.controllers.ContactsController and result update
at org.apache.struts2.rest.RestActionInvocation.findResult(RestActionInvocation.java:283) ~[struts2-rest-plugin-2.5.22.jar:2.5.22]
at org.apache.struts2.rest.RestActionInvocation.executeResult(RestActionInvocation.java:225) ~[struts2-rest-plugin-2.5.22.jar:2.5.22]
at org.apache.struts2.rest.RestActionInvocation.processResult(RestActionInvocation.java:189) ~[struts2-rest-plugin-2.5.22.jar:2.5.22]
at org.apache.struts2.rest.RestActionInvocation.invoke(RestActionInvocation.java:137) ~[struts2-rest-plugin-2.5.22.jar:2.5.22]
at com.opensymphony.xwork2.DefaultActionProxy.execute(DefaultActionProxy.java:157) ~[struts2-core-2.5.22.jar:2.5.22]
...
Here's the controller:
public class ContactsController implements ModelDriven<Object> {
private static final Logger log = LogManager.getLogger(ContactsController.class);
private String id;
private Contact model = new Contact();
private Collection<Contact> list;
private ContactsRepository contactsRepository = new ContactsRepositoryImpl();
@Override
public Object getModel() {
return (list != null ? list : model);
}
public void setId(String id) {
if (id != null) {
int contactId = Integer.parseInt(id);
this.model = contactsRepository.getContact(contactId);
}
this.id = id;
}
public HttpHeaders index () {
log.debug("Reading all contacts...");
list = contactsRepository.getContacts();
return new DefaultHttpHeaders("index").disableCaching();
}
...
// PUT /orders/1
public String update() {
log.debug("Updating existing contact(" + id + ")...", model);
contactsRepository.updateContact(model);
return "update";
}
...
... and 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.mapper.class" value="rest" />
<constant name="struts.convention.action.suffix" value="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="controllers"/>
<package name="contacts" extends="rest-default">
<global-allowed-methods>index,show,create,update,destroy,deleteConfirm</global-allowed-methods>
</package>
</struts>
Nothing worked before I added <package name="contacts">
. I tried several different variations of adding <action>
and <result>
, to no avail.
Example URLs:
GET Endpoint= http://localhost:8080, Resource= /StrutsContactsApp/contacts.json
: OKPOST Endpoint= http://localhost:8080, Resource= /StrutsContactsApp/contacts.json + JSON body
: OKPUT Endpoint= http://localhost:8080, Resource= http://localhost:8080, + JSON body
:API call succeeds, browser gets "HTTP 404"; log says "No result defined for action com.example.contactsapp.controllers.ContactsController and result update"
Q: Any suggestions for resolving the error and getting "update()" working with struts2-rest-plugin?
PS: I'm having similar problems with all three of "update()", "show()" and "destroy()". Each expect an id (which I can see in the Eclipse debugger is being passed in correctly).
Also:
The struts-rest-plugin supports URLs like http://localhost:8080/myapp/contacts.json
(for JSON) and http://localhost:8080/myapp/contacts.xml
(for XML). I'm not sure if "update()" needs contacts.json/id
, or just contacts/id
. Neither work :(