REST和非RESTful URL的在一起不起作用 [英] REST and non-RESTful URL's Together doesn't work

查看:134
本文介绍了REST和非RESTful URL的在一起不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有试图获得纯静态配置以及至少一个非静态URL的可行示例的所有尝试都失败了.

All my attempts to get a working example of both, pure restful configuration together with at least one non-restful URL failed.

我有此页面作为指南: https://cwiki.apache.org/confluence/display/WW/REST+Plugin

I have this page as a guide: https://cwiki.apache.org/confluence/display/WW/REST+Plugin

我有一个模型,Receipt有几个测试字段.

I have one model, Receipt with a few test fields.

纯REST解决方案可以按预期工作,因此请执行以下操作:

The pure REST solution works as expected, so doing this:

<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>

<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"/>

与以下结果配合良好:

receipt.json =>显示所有收据清单

receipt.json => display the list of all the receipts

receipt/1.json =>显示ID为1的收据

receipt/1.json => display the receipt with id = 1

但是,如果我尝试将宁静的URL与不宁静的URL混合使用,(就像我在Wiki文章中告诉的一样):

But if I try to mix restful with non-restful URLs, (the same way I'm told to in the wiki article) like this:

<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.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"/>

它炸毁了:

Struts has detected an unhandled exception:

Messages:   
com.immaculate.receipto.controllers.ReceiptController.execute()
File:   java/lang/Class.java
Line number:    1,778

我很困惑.为什么要搜索execute()? execute()应该是存在于动作上的方法.我没有给它一个动作,而是给了它一个像以前一样的控制器.

I am confused. Why is it searching for execute()? execute() should be a method present on actions. I'm not giving it an action, I'm giving it a controller like before.

我该如何解决?

这是ReceiptController:

public class ReceiptController implements ModelDriven<Object> {

    private ReceiptManager receiptManager = new ReceiptManager();
    private String id;
    private Receipt model = new Receipt();
    private Collection list;


    public Object getModel(){
        if(list==null){
            return model;
        } else {
            return list;
        }
    }

    public HttpHeaders create() {
        receiptManager.save(model);
        return new DefaultHttpHeaders("show");
    }


    public HttpHeaders show() {
        model = receiptManager.find(id);
        return new DefaultHttpHeaders("show");
    }

    public HttpHeaders destroy() {
        model = receiptManager.destroy(id);
        return new DefaultHttpHeaders("show");
    }


    public HttpHeaders index() {
        list = receiptManager.list();
        return new DefaultHttpHeaders("show");
    }

    public String getId() {
        return id;
    }

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

很明显,我在这里做错了.除了这里解释的内容,我是否还需要了解其他内容?

Obviously I'm doing something wrong here. Do I need to know something else besides what's explained here?

此外,我该如何阅读? :

Also, how do I read this? :

<constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts"/>

这是否意味着以/rest开头的所有内容都将映射到我的纯净宁静控制器,进而将其映射到我的收据,而没有/rest前缀的任何内容都将映射到默认的struts映射?还是我在这里完全走错了方向?

Does this mean that everything starting with /rest will map to my pure restful controller that will, in turn map to my receipt and anything without the /rest prefix will map to the default struts mappings? or am I going entirely in the wrong direction here?

推荐答案

基于前缀的操作映射器将查找映射委托给

Prefix based action mapper delegates finding mapping to the corresponding mapper defined by the

<constant name="struts.mapper.prefixMapping" value="/rest:rest,:struts"/>

这意味着在最后一个斜杠/之前的URL中具有/rest的所有URL都映射到其余映射器,其他映射到默认映射器.如果您有receipt控制器,则应使用值"/receipt:rest,:struts".

This means all URLs that have /rest in the URL before the last slash / are mapped to the rest mapper, others to the default mapper. If you have a receipt controller, then you should use the value "/receipt:rest,:struts".

参考:

  • REST Plugin
  • RestfulActionMapper
  • PrefixBasedActionMapper

这篇关于REST和非RESTful URL的在一起不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆