如何使用HTTP请求发送和接收QUERY参数

时间:2015-07-10 12:07:47

标签: java mysql spring http httprequest

我尝试使用http请求发送查询参数,但我不知道该怎么做,这是我的实际控制器

package com.iquest.news.controller;

import com.iquest.news.dao.AbstractGenericDao;
import com.iquest.news.entities.News;
import com.iquest.news.services.ServiceInterface;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class NewsController {

    @Autowired
    private ServiceInterface<News> newsService;
    Logger logger = Logger.getLogger(AbstractGenericDao.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showNews(Model model) {
        List<News> news = newsService.getAll();
        if (news.size() != 0) {
            model.addAttribute("news", news);
            logger.debug("CONTROLLER: News controller has executed with success");
            return "news";
        } else {
            return "error";
        }
    }

    @RequestMapping(value = "/startDate={date}", method = RequestMethod.GET)
    public String getNewsByDate(@PathVariable long date, Model model) {
        List<News> news = newsService.getNewsByDate(date);
        if (news.size() != 0) {
            model.addAttribute("news", news);
            logger.debug("CONTROLLER: News controller has delivered data (from GET NEWS BY DATE) with success");
            return "news";
        } else {
            return "error";
        }
    }

    @RequestMapping(value = "/startDate={date}/author={author}", method = RequestMethod.GET)
    public String getNewsByDateAndAuthor(@PathVariable long date, @PathVariable String author, Model model) {
        List<News> news = newsService.getNewsByDateAndAuthor(date, author);
        if (news.size() != 0) {
            model.addAttribute("news", news);
            logger.debug("CONTROLLER: News controller has delivered data (from GET NEWS BY DATE AND AUTHOR) with success");
            return "news";
        } else {
            return "error";
        }
    }
}

此处执行了http://localhost:8080/news/startDate=1436529204/author=v

后,我的网址链接

我如何使此网址看起来像:http://localhost:8080/news?startDate=1436529204?author=v或类似的东西。 有谁知道我怎么能这样做? 请求帮助:D

2 个答案:

答案 0 :(得分:2)

Actualy&#34; URL&#34;有多个问号(&#34;?&#34;)不是有效的网址。如果您正在查看如何访问有效网址的查询参数,例如http://localhost:8080/news?startDate=1436529204&author=v,那么您的方法签名应如下所示:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getNewsByDateAndAuthor(@RequestParam("date") Long date, @RequestParam("author") String author, Model model) {

答案 1 :(得分:1)

嗯,这是一种方法。

@RequestMapping(value={"/news"},method={org.springframework.web.bind.annotation.RequestMethod.POST,
            org.springframework.web.bind.annotation.RequestMethod.GET})
    public String getQueryParams(final Model model,final HttpServletRequest request){
String startDate= request.getParameter("startDate");
String author= request.getParameter("author");
}