草庐IT

java - liferay spring portal @ResourceMapping 无法触发不同类型的http方法

coder 2024-03-12 原文

我使用 SpringthymeleafAngularJS 创建了一个 liferay portlet 应用程序。对于 AngularJS 和 spring 之间的通信,我需要创建一些我使用 @ResourceMapping 创建的 rest 调用,如下所示。该应用程序工作正常,但问题是我不知道如何制作 GETDELETEPUT http REST 调用,因为 @ResourceMapping 不允许指定任何方法。

@ResourceMapping(value="getUserDetail")
public void userDetail(@RequestParam long userId, ResourceResponse response) throws Exception {
    Users users = new Users(userId);
    // some logic 

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    JSON_MAPPER.writeValue(response.getPortletOutputStream(), users);
}

当我使用 @RequestMapping 而不是 @ResourceMapping 时,如下所示

@RequestMapping(value="getUserDetail", method=RequestMethod.GET)
@ResponseBody
public void userDetail(@RequestParam long userId, ResourceResponse response) throws Exception {
    System.out.println("Got detail request for user with id {} "+ userId);

    // UserDetail userDetail = this.userService.getPortalUserDetail(userId);
    List<String> users = new ArrayList<String>();
    users.add("Manu");
    users.add("Lissie");
    users.add("John");
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    JSON_MAPPER.writeValue(response.getPortletOutputStream(), users);
}

我有

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Mode mappings conflict between method and type level: [getUserDetail] versus [view]

谁能告诉我一些解决办法

  1. 如何使用 @ResourceMapping 创建不同类型的 http 调用
  2. 我们能否在 Liferay Spring portlet 中使用 @RequestMapping 而不是 @ResourceMapping 进行 REST 调用
  3. 我们如何创建基于资源的 REST url,例如 getUser/12/mumbai
  4. 我们如何发送 REST json 作为 body 而不是 Request Param

最佳答案

模式映射冲突异常

问题没有显示,但您的 Controller 可能有 @RequestMapping("view") 注释。此类型级别映射与方法级别映射冲突。您应该删除 Controller 类上的 @RequestMapping 注释。

请求映射示例

@Controller
public class SampleRESTFullController {

// Simple GET
@RequestMapping(value = "/helloSample", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<HelloSample> helloSample() { ... }

// GET with path variable
@RequestMapping(value = "/helloSample/sampleId/{sampleId}", method = RequestMethod.GET)
public @ResponseBody HelloSample helloSample(@PathVariable("sampleId") Long sampleId) { ... }

// POST with @RequestBody
@RequestMapping(value = "/helloSample", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody HelloSample createSample(@RequestBody HelloSample helloSample) { ... }

// PUT with path variable and @RequestBody
@RequestMapping(value = "/helloSample/sampleId/{sampleId}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.NO_CONTENT)
void update(@PathVariable("sampleId") long sampleId, @RequestBody HelloSample helloSample) { ... }

// DELETE
@RequestMapping(value = "/helloSample/sampleId/{sampleId}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
void delete(@PathVariable("sampleId") long sampleId) { ... }

}

我从 Using RESTFul services with Liferay 中获取示例博客文章。它回答了您所有的问题并提供了大量示例。注意 Spring 配置,这使得 RESTful 服务成为可能(尤其是 View 解析器和消息转换器)。

关于java - liferay spring portal @ResourceMapping 无法触发不同类型的http方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33167463/

有关java - liferay spring portal @ResourceMapping 无法触发不同类型的http方法的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  6. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  7. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  10. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

随机推荐