Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to get request's URI from WebRequest in Spring?

 
Ranch Hand
Posts: 640
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am handling REST exceptions using @ControllerAdvice and ResponseEntityExceptionHandler in a spring Rest webservice. So far everything was working fine until I decided to add the URI path(for which exception has occurred) into the BAD_REQUEST response.

ApiError is just a Pojo class:
But WebRequest has not given any api to get the path for which the request failed. I tried:
request.toString() returns -> ServletWebRequest: uri=/signup;client=0:0:0:0:0:0:0:1
request.getDescription(false) returns -> uri=/signup
getDescription is pretty close to the requirement, but doesn't meet it. Is there any way to get only the uri part?
 
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try casting it to ServletWebRequest (that should work unless it's a FacesWebRequest - only possible if you're using JSF), then call its getRequest method.
 
Puspender Tanwar
Ranch Hand
Posts: 640
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Try casting it to ServletWebRequest (that should work unless it's a FacesWebRequest - only possible if you're using JSF), then call its getRequest method.

This worked.
 
Rob Spoor
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use getRequestURI()? Unless you need the host that will give the same information but as a String.
 
A feeble attempt to tell you about our stuff that makes us money
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!