将数组或列表传递给@Pathvariable - Spring/Java [英] Passing an Array or List to @Pathvariable - Spring/Java

查看:95
本文介绍了将数组或列表传递给@Pathvariable - Spring/Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 JBoss/Spring 中做一个简单的获取".我希望客户端在 url 中传递给我一个整数数组.我如何在服务器上设置它?并显示客户端是否应该发送消息?

这就是我现在所拥有的.

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)@ResponseBodypublic String test(@PathVariable List firstNameIds){//我该怎么办??返回 "Dummy";}

在客户端我想传递类似

<前>http://localhost:8080/public/test/[1,3,4,50]

当我这样做时,我得到一个错误:

<块引用>

java.lang.IllegalStateException:在@RequestMapping 中找不到@PathVariable [firstNameIds]

解决方案

GET http://localhost:8080/public/test/1,2,3,4@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)@ResponseBody公共字符串测试(@PathVariable String[] firstNameIds){//firstNameIds: [1,2,3,4]返回 "Dummy";}

(使用 Spring MVC 4.0.1 测试)

I am doing a simple 'get' in JBoss/Spring. I want the client to pass me an array of integers in the url. How do I set that up on the server? And show should the client send the message?

This is what I have right now.

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable List<Integer> firstNameIds)
{
     //What do I do??
     return "Dummy"; 
}

On the client I would like to pass something like

http://localhost:8080/public/test/[1,3,4,50]

When I did that I get an error:

java.lang.IllegalStateException: Could not find @PathVariable [firstNameIds] in @RequestMapping

解决方案

GET http://localhost:8080/public/test/1,2,3,4

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable String[] firstNameIds)
{
    // firstNameIds: [1,2,3,4]
    return "Dummy"; 
}

(tested with Spring MVC 4.0.1)

这篇关于将数组或列表传递给@Pathvariable - Spring/Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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