Spring Boot REST Template URI Encoding
Join the DZone community and get the full member experience.
Join For FreeHard coding a URI endpoint for the Rest template interface is preferably the first point of reference when implementing a micro service or a monolith application.
There is no easy way to do this!
URI variables are usually meant for path elements, query string parameters, or when you decouple your application from your front-end. Ideally, you'd find a better solution for constructing your URI to access data on various endpoints. But, after doing a couple of endpoint hits, you'll probably run into a funny looking error like
org.springframework.web.client.ResourceAccessException: I/O error: http://localhost:8080%api%users
Regardless of all the research, the URI package comes in quite handy with String Builder to Query a API. There are a few ways of implementing this:
StringBuilder builder = new StringBuilder("http://localhost:8080/api/users");
URI uri = URI.create(builder.toString());
List<Users> updatedUsers = restTemplate.getForObject(uri, List.class);
This returns a list of users on a GET request in the Rest Template. If you're using the POST request, append your parameters in a string builder like in the snippet below:
xxxxxxxxxx
StringBuilder builder = new StringBuilder("http://localhost:8080/api/adduser");
builder.append("?username=");
builder.append(URLEncoder.encode("john@1.com",StandardCharsets.UTF_8.toString()));
builder.append("&password=");
builder.append(URLEncoder.encode("P@$$word",StandardCharsets.UTF_8.toString()));
builder.append("&phone=");
builder.append(URLEncoder.encode("911",StandardCharsets.UTF_8.toString()));
URI uri = URI.create(urlencoded);
restTemplate.postForObject(uri, Users.class);
If using special characters, you might want to use the URL encoder on specific charsets. In this case, we're using UTF_8. This is a huge question mark around just submitting a hard coded URL or how the Rest template behaves.
OR the easiest way is to simply just submit an object on a hard coded URI for your POST Query.
xxxxxxxxxx
value = "/addNewUser", method = RequestMethod.POST) (
public ModelAndView processRequest(HttpServletRequest request, HttpSession session, ("username")String username, ("password")String password, ("phone")String phone) {
RestTemplate restTemplate = new RestTemplate();
Users user = new Users();
user.setUsername(username);
user.setPassword(password);
user.setPhone(phone);
user.setEnabled(true);
restTemplate.postForObject("http://localhost:8080/api/adduser", user, Users.class);
StringBuilder builder = new StringBuilder("http://localhost:8080/api/users");
URI uri = URI.create(builder.toString());
List<Users> updatedUsers = restTemplate.getForObject(uri, List.class);
session = request.getSession(false);
ModelAndView model = new ModelAndView();
if(session != null){
model.setViewName("welcome");
model.addObject("users", updatedUsers);
model.addObject("user", (Users)session.getAttribute("user"));
model.addObject("sessiontracker", sessiontracker.getList());
model.addObject("sumValidSessions", sessiontracker.getSizeSessionList());
return model;
}
return new ModelAndView("login");
}
The GUI sample i'm using adds a user and displays it in a user table.
So, it all depends on which version of Spring you're using. If you're using an old version of Spring, the UriComponentsBuilder
with your spring-web jar wont be included. For more information on standard URI 's and encoding click on this link: Java URL Encoding.
Delving into URI 's can get quite complex with Rest Template. Nevertheless, I'll recommend this for a monolithic application or If you want a quick metrics GUI around your microservices data. Don't disregard that Spring allows you to develop two Controller types, a standard MVC Controller, and a Rest Controller, which gives you the capabilities of exposing an API.
It is favorable nonetheless to use an HTTP client for B2B communications on URLs in a microservice environment, which adds much more customization around exceptions and Response codes.
I hope this helps to find traction on the route to take and I hope this gives you some variance around Spring Boot Rest API.
Thank you!
Opinions expressed by DZone contributors are their own.
Comments