在其余服务中修改GET的网址路径

时间:2019-11-13 13:04:25

标签: java spring rest

我实现了一个简单的rest GET服务,并且想修改该服务的ulr。

现在的网址是:http://localhost:8011/types/id?date=2019-07-30T11:35:42

我想添加一个过滤器,并在日期中添加一些括号[]

像这样:http://localhost:8011/types/id过滤器[日期] = 2019-07-30T11:35:42

这是我的Get服务,在值中我具有“ types / id”,但我不知道如何为请求的参数添加过滤器和括号。

    @RequestMapping(value = "types/id", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<?> getTicketids( @RequestParam(required = false) String date)
    {
        ...
    }

对于我可以更改或应阅读的内容的任何建议,我将不胜感激。

3 个答案:

答案 0 :(得分:1)

我假设您正在使用Spring定义RestController。通常,参数应如下所示:

public ResponseEntity<?> getTicketids( @RequestParam(name = "filterDate", required = false) String date)
    {
        ...
    }

此代码允许请求http://localhost:8011/types/id?filterDate=mydate

但是,方括号是not allowed in an URL,因此您可能需要重新考虑该特定方法。

答案 1 :(得分:0)

为什么要在RequestParam中添加方括号?是因为您期望使用多种过滤器并希望区分它们?在这种情况下,您可以改为添加两个请求参数-

handleDeleteOfImages = (ids, name, destinationFolder, downloadImages) => {
  downloadImages(ids, name, destinationFolder).then(addResponses => {
    <---some code --->
  });
};
handleDownloadOfImages = (
  downloadImages,
  name,
  absFolderPath,
) => {
  return new Promise(resolve => {
    const cursor = this.state.cursor;
    this.context.client
      .query({
        query: SearchQuery,
        variables: {
          ...(cursor && { cursor }),
          types: 'Images',
          query: `in:"${absFolderPath}"`,
          sortBy: 'dateAsc',
          limit: 5,

        }
      })
      .then(({ data }) => {

        const images = get(data, 'search.images');

        const more = get(data, 'search.more');

        this.setState({
          cursor: {
            id: images[images.length - 1].id,
            time: images[images.length - 1].time
          }
        });
        const ids = images && images.map(message => message.id);

        if (ids) {
          this.handleDeleteOfImages(
            ids,
            name,
            destinationFolder,
            downloadImages,
          );
        }
        resolve (more)
      });
  });
};

handleMoveToDatabase = async (sourcefolder, destinationFolder) => {
  const { name, absFolderPath } = folder;
  const downloadImages= this.props.downloadImages;
  let more = false;

  do {
    more = await this.handleDownloadOfImages(
      downloadImages,
      absFolderPath
    name,
      destinationFolder
  );

  } while (more === true);
};

另外,请确保这些值经过url编码,以避免字符串的任何部分使您的url无效。

希望,这会有所帮助。

答案 2 :(得分:0)

建议您不要像这样在URL中使用方括号,而应通过以下列表:

http://localhost:8011/types/id?filters=firstValue,secondValue,thirdValue

并像这样更改您的控制器:

@RequestMapping(value = "types/id", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void receiveArrayOfValues(@RequestParam String[] filters) 
{
  // Handle values here
}