CURL GET请求和Spring Mvc请求映射问题

时间:2012-10-23 14:20:11

标签: json spring-mvc curl spring-roo

我正在尝试使用CURL获取以下方法:

@RooWebJson(jsonObject = Geolocation.class)
@Controller
@RequestMapping("/geolocations")
public class GeolocationController {

    @Autowired
    private GeolocationRepository geolocationRepository;

    @RequestMapping(params = "findByPostcodeFirstChars", method = RequestMethod.GET, headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<String> jsonFindGeolocationsByPostcodeFirstChars(@RequestParam("postcodeFirstChars") String postcodeFirstChars) {
        System.out.println("jsonFindGeolocationsByPostcodeFirstChars");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<Geolocation> geolocations = geolocationRepository.findByPostcodeStartingWith(postcodeFirstChars);
        return new ResponseEntity<String>(Geolocation.toJsonArray(geolocations), headers, HttpStatus.OK);
    }
}

我使用CURL如下:

 curl -i -H Accept:application/json http://localhost:8080/kadjoukor/geolocations?findByPostcodeFirstChars%26postcodeFirstChars=7500

但是,上面的CURL命令总是获取此方法(来自Roo ITD)而不是上面的方法:

@RequestMapping(headers = "Accept=application/json")
    @ResponseBody
    public ResponseEntity<String> GeolocationController.listJson() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=utf-8");
        List<Geolocation> result = geolocationRepository.findAll();
        return new ResponseEntity<String>(Geolocation.toJsonArray(result), headers, HttpStatus.OK);
    }

我不确定我错了什么。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

我的CURL语法错误。

我不应该尝试手动编码&符号。这是问题的原因。

以下是发送GET请求的适当方式:

curl -i -X GET -H Accept:application/json "http://localhost:8080/kadjoukor/geolocations?findByPostcodeFirstChars&postcodeFirstChars=7500"

注意引号和&amp;