This code is giving me headache. Maybe this is a lame question. I have been using REST API for some time. But I have never used URI and unable to understand why we are using URI here. Why do we need to URI object into the response?
This method is using post mapping.
#PostMapping("/user")
long gameId = gameService.createGame(gamePostDTO.getPlayerIds());
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.buildAndExpand("1")
.buildAndExpand(String.format("%d", gameId))
.toUri();
return ResponseEntity.created(location).build();
}
Related
i have a request with empty path variable that get me a 404 response.how can i get my GET request to go into my webservice and throw exception showing that the pathvariable is empty. for now i'm having a 404 not found exception. exemple : GET
request url : hello/123/me ==> works fine
request url hello//me ===> 404 not found
#GetMapping(value = "/hello/{uuid}/me")
public ResponseEntity<Void> hello(
#PathVariable(name = "uuid", required = false) String uuid){
You can achieve it with the following code.
#GetMapping(value = {"/hello/{uuid}/me", "/hello//me"})
public ResponseEntity<Void> hello(#PathVariable(name = "uuid", required = false) String uuid) {
What I have done is, just added /hello//me also to the request mapping of the same function. You can add multiple URLs just like this.
I suggest making the URL without parameters as /hello/me just to make it a standard, obviously if you have a scenario where you want to achieve it with /hello//me, you can do that.
Maybe this the answer you looking for...., basically you want to pass the value of that uuid to be checked if its valid or not. If its not you can pass the exception into the web service using IllegalStateException. If you using getter and setter with JPA in your spring this can be done :
Optional<ClassName> validUuid = NameObjectofYourJPARepo.findByUuid(YourClassName.getUuid());
if(!validUuid.isPresent()) {
throw new IllegalStateException("404 not found");
}
I hope this can answer your problem
I am using Volley for my HTTP requests and I have an HTTP put URL which looks like below.
http://mycompany.com/favorite/{roomNumber}/count. I am using a JSON object request. How do I make the API work with the extra "/count" in the API? I am passing the parameter room number in the JSON object.
JSON Object request works fine with this type of URL "http://mycompany.com/favorite/{roomNumber}"
JSON Object request
JsonObjectRequest request = new JsonObjectRequest(METHOD_TYPE_PUT, url, jsonObjectParams, responseListener, errorListener)
Can somebody help me with passing the JSON object parameter in the middle of the URL
Thanks.
You can call the API dynamically like this,
private void getTheApiData(int roomNumber){
JsonObjectRequest request = new JsonObjectRequest(METHOD_TYPE_PUT,
"mycompany.com/favorite" + roomNumber + "/count",
jsonObjectParams, responseListener, errorListener)
}
and call the above API dynamically by the method when you get the new data every time like this.
getTheAPiData(20) //if room number is 20
let me know if you have any issue #Shravani
I have started using WebClient(org.springframework.web.reactive.function.client.WebClient) for calling rest services. I have 2 classes say Utility.java and ServiceImpl.java.
ServiceImpl.java is where I use WebClient. A post call I am making looks like -
ClientResponse response = webClient.post()
.uri(path)
.body(Mono.just(inputDTO),InputDTO.class)
.exchange()
.block();
(ClientResponse above is org.springframework.web.reactive.function.client.ClientResponse)
(I am using exchange instaed of retrive because I want headers as well as status code)
Now trying to convert this response into some DTO - ResponseDTO.
Mono<ResponseEntity<ResponseDTO>> mono = response.toEntity(ResponseDTO.class);
ResponseEntity<ResponseDTO> resEntity = mono.block();
ResponseDTO myObj = resEntity.getBody();
So myObj is an object of ResponseDTO class.
The issue is - when I perform the conversion of 'response into ResponseDTO.java' in my utility class, I get myObj = null. But if I do it in my ServiceImpl.java (just after calling post API), it returns the proper body (ResponseDTO object).
The same issue occurs even if I perform the conversion and post call operation in two different methods in the ServiceImpl.java.
Do I need to configure something here?
I figured out what was the issue.
After calling REST api, body in the response if flushed out after I read it from the response for the first time. I had a Sysout statement in service implementation class where I was reading the body content.
Recommendation: Read the body content only once and store it in a variable. Use it wherever required.
For some reason I need to call a GET method API and pass json request body for it. I really couldn't find an example for it. I wonder if it is even supported using feign.
How can I do that using feign?
Yes, Feign supports it. You can do the same as with POST requests:
#FeignClient(name = "clientName", url = "http://localhost:8888")
public interface SampleFeignClient {
#GetMapping("/remote")
String test(#RequestBody SampleRequestBody sampleRequestBody);
}
But be aware: a lot of servers ignore body or even refuse that kind of "non-standard" requests completely (GET or HEAD with request bodies).
According to the documentation the correct way to do it would be to use the #SpringQueryMap annotation.
#FeignClient(name = "clientName", url = "http://localhost:8888")
public interface SampleFeignClient {
#GetMapping("/remote")
String test(#SpringQueryMap SampleRequestBody sampleRequestBody);
}
You can find more information here
#RequestMapping(value = "/{Id}", method = RequestMethod.GET)
public String searchCity( #PathVariable ("Id") Long Id) {
mymethod.something(id);
return "successpage";
}
When I'm trying to write some number it print me Eror 404 resource is not available, but when I write {Id} ("7DId7D" smthing like this) it redirects me to succespage, What is the problem? Help me please...
The information you provided conflicts with known behavior of Spring MVC
http://localhost:8080/MyCountryProject/7 should maps to searchCity fine
http://localhost:8080/MyCountryProject/%7Bld%7D should not even map to searchCity
I would check following to further isolate the problem:
Are you sure you're testing against the right controller? If your controller has #RequestMapping("myController") then your URL would be http://localhost:8080/MyCountryProject/MyController/7
Are you sure you're posting with the correct HTTP method? If you're HTTP form is POST, it wouldn't map to searchCity method
The conversion from string into Long is done by Spring MVC builtin property editor, did you install your own property editor? If so debug this