Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.servlet.http.HttpServletRequest;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpMethod;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
- @RestController
- public class AddContactController {
- private static final Logger log = LoggerFactory.getLogger(ListController.class);
- @RequestMapping(value="/add", method=RequestMethod.POST)
- public String addContact(HttpServletRequest request, @RequestBody Contact contact) {
- String url = Constants.CONTACTS_ADD_URL;
- String username = request.getHeader("auth-user");
- String password = request.getHeader("auth-password");
- HttpHeaders headers = new HttpHeaders();
- if (username != null && password != null){
- CreateBasicAuth creds = new CreateBasicAuth(username, password);
- headers.set("Authorization", "Basic "+ creds.base64creds());
- }
- headers.setContentType(MediaType.APPLICATION_JSON);
- headers.set("Accept", "application/x-spring-data-verbose+json");
- HttpEntity<Contact> entity = new HttpEntity<Contact>(contact,headers);
- RestTemplate restTemplate = new RestTemplate();
- try {
- ResponseEntity<Contacts> response = restTemplate.exchange(url,
- HttpMethod.POST, entity, Contacts.class);
- return "Status: " + response.getStatusCodeValue();
- } catch (Exception e)
- {
- log.info(e.getMessage());
- return "Error while processing your request";
- }
- }
- }
Add Comment
Please, Sign In to add comment