我正在使用 struts2-rest-plugin 编写一个 REST 服务器。我正在使用 SoapUI 和 Postman 测试 REST 调用。我在这里关注示例代码:
https://cwiki.apache.org/confluence/display/WW/REST+Plugin
我的 REST 要求controller.index()
)并且controllers.create()
工作正常。
但是虽然controller.update()
成功调用了控制器并更新了记录:
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)...
...
...它在“返回”时失败,出现以下错误:
...
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]
...
这是控制器:
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";
}
...
...和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>
在我添加之前没有任何效果<package name="contacts">
。我尝试了几种不同的添加<action>
和<result>
,但无济于事。
示例网址:
GET Endpoint= http://localhost:8080, Resource= /StrutsContactsApp/contacts.json
: 好的POST Endpoint= http://localhost:8080, Resource= /StrutsContactsApp/contacts.json + JSON body
: 好的PUT Endpoint= http://localhost:8080, Resource= http://localhost:8080, + JSON body
:API调用成功,浏览器获取“HTTP 404”;日志显示“没有为操作 com.example.contactsapp.controllers.ContactsController 和结果更新定义结果”
问:关于解决错误和让“update()”与 struts2-rest-plugin 一起工作的任何建议?
PS:我在“update()”、“show()”和“destroy()”这三个方面都遇到了类似的问题。每个都期望一个 id(我可以在 Eclipse 调试器中看到它被正确传递)。
还:
struts-rest-plugin 支持像http://localhost:8080/myapp/contacts.json
(for JSON) 和http://localhost:8080/myapp/contacts.xml
(for XML) 这样的 URL。我不确定“update()”是否需要contacts.json/id
,或者只是contacts/id
。都不工作:(