Java 如何使用HTTP请求发送和接收查询参数

Java 如何使用HTTP请求发送和接收查询参数,java,mysql,spring,http,httprequest,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 o

我试图用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";
        }
    }
}
package com.iquest.news.controller;
导入com.iquest.news.dao.AbstractGenericDao;
导入com.iquest.news.entities.news;
导入com.iquest.news.services.ServiceInterface;
导入org.apache.log4j.Logger;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入java.util.List;
@控制器
公共类新闻控制器{
@自动连线
专用服务接口新闻服务;
Logger=Logger.getLogger(AbstractGenericDao.class);
@RequestMapping(value=“/”,method=RequestMethod.GET)
公共字符串showNews(模型){
List news=newservice.getAll();
如果(news.size()!=0){
model.addAttribute(“新闻”,新闻);
调试(“控制器:新闻控制器已成功执行”);
返回“新闻”;
}否则{
返回“错误”;
}
}
@RequestMapping(value=“/startDate={date}”,method=RequestMethod.GET)
公共字符串getNewsByDate(@PathVariable long date,Model){
List news=newservice.getNewsByDate(日期);
如果(news.size()!=0){
model.addAttribute(“新闻”,新闻);
debug(“控制器:新闻控制器已成功交付数据(从GET News BY DATE获得数据”);
返回“新闻”;
}否则{
返回“错误”;
}
}
@RequestMapping(value=“/startDate={date}/author={author}”,method=RequestMethod.GET)
公共字符串getNewsByDateAndAuthor(@PathVariable long date,@PathVariable字符串作者,模型){
列表新闻=新闻服务。getNewsByDateAndAuthor(日期,作者);
如果(news.size()!=0){
model.addAttribute(“新闻”,新闻);
debug(“控制器:新闻控制器已成功交付数据(从按日期和作者获取新闻”);
返回“新闻”;
}否则{
返回“错误”;
}
}
}
这是我执行后的URL链接:

我如何使这个URL看起来像:或类似的东西。 有人知道我怎么做吗?
Thx帮助:D

好吧,这里有一个方法

@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");
}
实际上,带有多个问号(“?”)的“URL”不是有效的URL。如果要查看如何访问有效URL的查询参数,如,则方法签名应如下所示:

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

看一看,实际上您必须绑定请求参数,而不是路径参数。如果我使用了这个链接,并尝试使用localhost:8080/news?startDate=1436531644&author=v,这是我的结果,我不知道为什么不起作用:每次我使用这个链接时,他都会自动改变,你知道我能做什么吗?我相信你的dispatcher servlet映射有问题,请分享一下。您可以在控制台“未找到映射”上看到相同的错误。如果我使用该方法,并使用以下链接:localhost:8080/news?startDate=1436531644&author=v他会自动更改我,我不知道为什么,您知道吗?