1

I have created a REST api which can used to save different urls those url have auto-increment feature that assign them an id one endpoint is to add urls and other is to fetch urls from id I want to do something like if I pass localhost:8080/getUrlById?id=4/ my browser should redirect me to that url which is there at 4th no.

my controller code -

@GetMapping("/addUrl")
public ResponseEntity<?> addUrlByGet(String url) {
    return new ResponseEntity<>(sortnerService.addUrlByGet(url),HttpStatus.OK);
}


@GetMapping("/findUrlById")
    public ResponseEntity<?> findSortnerById(Integer id){
    return new ResponseEntity<>(sortnerService.findUrlById(id), HttpStatus.OK);
}

service class -

@Service
public class SortnerService {
    @Autowired
    private  SortnerRepo sortnerRepo;

    public Sortner addUrlByGet(String url) {
        Sortner sortner = new Sortner();
        sortner.setUrl(url);
        return  sortnerRepo.save(sortner);
    }

//   finding by particular Id

   public List<Sortner> findUrlById(Integer id){
        return sortnerRepo.findSortnerById(id);
   }
}

Can anyone suggest me any way to do it I am really new to SpringBoot Sorry if I have made any silly mistake.

Chitrang Sharma
  • 252
  • 3
  • 14
  • Does this answer your question? [Spring MVC @RestController and redirect](https://stackoverflow.com/questions/29085295/spring-mvc-restcontroller-and-redirect) – VadymVL May 19 '21 at 14:48
  • Yes I was looking into this but here most of the answers are simply redirecting to some static url not from the id chosen by the client. – Chitrang Sharma May 19 '21 at 14:52
  • @VadymVL can you please suggest me some way how to redirect to the particular link that is existing in the db as long as user ask for url at id=1 the respective url should redirect I hope you are getting my point ! – Chitrang Sharma May 19 '21 at 14:54
  • What does a `Sortner` class? You need to convert you Sortner class to a URL – VadymVL May 19 '21 at 14:56
  • Sortner is nothing just a weird name (maybe it means something idk) its model class contain two private variables id which is autoincrement and url to set and get url , getter and setters and nothing much, – Chitrang Sharma May 19 '21 at 14:59

1 Answers1

3

Based on the information from the comments, I suggest that the Sortner class looks like this:

  public class Sortner {
    Long id;
    URL url;
  }

So to redirect to the URL by the Id from your service you need to rewrite your controller to look like this:

  @GetMapping("/findUrlById")
  public void findSortnerById(Integer id, HttpServletResponse response) throws IOException {
    List<Sortner> urls = sortnerService.findUrlById(id);
    if(urls != null && urls.size() > 0) {
        response.sendRedirect(urls.get(0).getUrl().toString());
    }
    response.sendError(HttpServletResponse.SC_NOT_FOUND)
  }
VadymVL
  • 5,366
  • 3
  • 26
  • 41
  • Thank you soo much I think now I got the idea how this works, This really means a lot :D just last thing its url is just appended with my application url like - localhost:8080/google.com is there any solution :) sorry for asking too much Thanks again – Chitrang Sharma May 19 '21 at 15:22
  • 1
    To prevent appending to your application URL like - `localhost:8080/google.com`, you need to ensure that the URL that you are redirecting has a schema section `http://` or `https://`. If t doesn't, you need to add it manually before calling `sendRedirect`. For example `response.sendRedirect("https://google.com");` – VadymVL May 19 '21 at 17:16
  • If you are calling this API as a document from a browser, the redirection will work. However, if this GET API call comes from Javascript, the redirection will not work. – justthink May 21 '21 at 13:19