Aman
Aman

Reputation: 3261

How to pass url as rest service?

I am trying to build a rest service in spring boot to update my database..

 @RequestMapping(value = "/setrepacking/{transaction_number}/{image_url}", method = RequestMethod.GET)
   public String setRepackingDetails(@PathVariable String transaction_number,
                                     @PathVariable  String image_url) {

    dao.setRepackingDetails(transaction_number, image_url);
    return "Updated repacking details for "+transaction_number;
   }

But my image_url is like below: And I want to pass below as part of rest component

http://xxxx.com/api/images/get?format=src&type=png

I am trying something like below:

`www.localhost:8080/setrepacking/3500574684/http://thecatapi.com/api/images`/get?format=src&type=png

It is not accepting...

How do I pass the parameter in my broswer??
Appricate any quick solution....

Upvotes: 1

Views: 186

Answers (2)

Éva Szloboda
Éva Szloboda

Reputation: 31

You create a new class or record with transactionNumber and imageUrl properties:

@AllArgsConstructor
public class PackingDetail {
    private final String transactionNumber;
    private final String imageUrl;
}

Then a PUT request where you send this object as request body:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@PutMapping("/api/repackings")
public void repacking(@RequestBody PackingDetail packingDetail) {   
    yourService.repacking(packingDetail);        
}

Upvotes: 1

Paulius Matulionis
Paulius Matulionis

Reputation: 23413

You have to URL encode your image URL path variable before passing it in the request, encoded URL looks like this:

http%3A%2F%2Fxxxx.com%2Fapi%2Fimages%2Fget%3Fformat%3Dsrc%26type%3Dpng

So you request has to look like this:

http://localhost:8080/setrepacking/3500574684/http%3A%2F%2Fxxxx.com%2Fapi%2Fimages%2Fget%3Fformat%3Dsrc%26type%3Dpng

This way you will get your image URL correctly. Also have a look at URLEncoder and URLDecoder

Upvotes: 1

Related Questions