我尝试发送与HTTP请求的查询参数,但我不知道该怎么做,这是我的实际CONTROLER如何发送和recive与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";
}
}
}
而且这里是我的URL链接执行后:http://localhost:8080/news/startDate=1436529204/author=v
我如何使这个URL看起来像︰http://localhost:8080/news?startDate=1436529204?author=v或类似的东西。 有没有人有任何想法我可以做到这一点? Thx求救:D
看看[这里](https://spring.io/guides/gs/rest -service /) –
实际上您必须绑定请求参数,但不绑定路径参数。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam –