У меня проблема с кодировкой при отправке запроса AJAX на конечную точку API.
У меня есть эта конечная точка в приведенном ниже коде с использованием Java Spring:
@Autowired
ApiKeyRepository apiKeyRepository;
@RequestMapping(value= "/weather/{cityName}/{fuCity}/now", method = {RequestMethod.GET}, produces="application/json" )
public ResponseEntity<Weather> getWeatherNowByCityName(@PathVariable(value = "cityName") String cityName, @PathVariable(value = "fuCity") State fuCity) throws JSONException, ParseException, java.text.ParseException {
String newCityName = cityName.toLowerCase();
try {
newCityName = URLDecoder.decode(newCityName , "UTF-8").replace(" ", "%20");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String weatherEndpoint = "/api/v1/locale/city?name=" + newCityName + "&state=" + fuCity.toString();
String appToken = apiKeyRepository.getByservice("climaTempo");
URL weatherDomain = new URL("http://apiadvisor.climatempo.com.br" + weatherEndpoint + "&token=" + appToken);
/// From here I send a JSON Request to the 'weatherDomain' to get the Weather from the city and her state that I get from the EndPoint Parameters
}
И я отправляю этот запрос jQuery Ajax на конечную точку:
var uf = $("#colWeather #selectState").val();
var city = $("#colWeather #selectCity").val();
$.ajax({
url: host + '/weather/' + city + '/' + uf + '/now',
type: 'GET',
contentType: "application/x-www-form-urlencoded; charset=utf-8",
async: true
}).done(function (JSONReturn) {
//Actions with JSONReturn
});
Но здесь, в Бразилии, у нас есть города с акцентами и cedilla, такие как «Avaí» из «SP», «Mairiporã» из «SP» и «Missão Velha» из «CE».
Если я отправлю конечной точке URL-адрес, такой как «/ weather / Americana / SP / now» или «/ weather / Piracicaba / SP / now», конечная точка без проблем вернет JSON-ответ.
Но если я отправлю конечной точке URL-адрес, такой как "/ weather / Mairiporã / SP / now" или "/ weather / Avaí / SP / now", API-интерфейс ClimaTempo возвращает нулевой JSON, и я получаю исключение NullPointerException.
Я думаю, что это проблема с акцентами, но я не могу отправить просто "/ weather / Mairipora / SP / now" без акцентов, потому что API ClimaTempo требует, чтобы название города совпадало с акцентами, в противном случае возвращается ноль JSON ...
Что я делаю не так?