How can I see the json coming from the client when using Spring-MVC? - spring

A client software is trying to access my Spring-MVC rest server, but it's getting a 400 (Bad Request) response every time. I know my server is fine (it's in use by many other clients), but I cannot debug the client application, so I cannot see what it is sending.
Is there a way for me to see what JSON I am receiving before Spring tries to convert it to an entity and fails? It's okay if I can only do this at debug time, I just need to be able to give support to this application's creators.
Just in case, here is the spring-mvc controller method:
#Named
#RequestMapping(value = "/taskmanager/task")
public class TaskManagerTaskRest {
#RequestMapping(value = "", method = RequestMethod.POST)
#ResponseBody
public void createTask(#RequestBody Task task, HttpServletRequest request,
HttpServletResponse response) throws CalabacinException {
// This code never gets executed because the Task json is invalid, but I don't know how I could see it.
...
...
}
}

Try to use Fiddler. It will help you to catch HTTP requests/responses. You will be able to see your JSON.

You can create and use a AbstractRequestLoggingFilter filter implementation and conditionally log the relevant parts of the request. You should use ContentCachingRequestWrapper to wrap the request.

Related

HttpServletRequest throws error when used within Aspect

I have a method which has an aspect. When I try to #Autowire HttpServletRequest, and use request.getHeader(something), I get this error -
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
How do I fix this? I tried using RequestContextHolder, but upon debugging I still see null. How do I use the RequestContextListener when my project has no web.xml.
Request Header can be accessed using HttpServletRequest below way.
private static HttpServletRequest getRequest() {
return((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
}
public static String getApiTraceId() {
return getRequest().getHeader(something);
}
Aspect annotations spins a new thread which is different from the one httpservlet is available in. This is why request was not available within the #ASpect. To resolve it, call the request object BEFORE the aspect method, cache it and call the same method as before.

HTTP Basic Authentication in Spring Web Services Integration Tests

I have a SOAP web service written in Spring Web Services that I would like to integration test.
I would like to use spring-ws-test as the reference documentation points to. So, the test code is similar to the example in the reference, something like that:
#Autowired
private ApplicationContext applicationContext;
private MockWebServiceClient mockClient;
#Before
public void createClient() {
mockClient = MockWebServiceClient.createClient(applicationContext);
}
#Test
public void customerEndpoint() throws Exception {
Source requestEnvelope = new ResourceSource(new ClassPathResource("request.xml"));
Source responsePayload = new ResourceSource(new ClassPathResource("response.xml"));
mockClient.sendRequest(withSoapEnvelope(requestPayload)).
andExpect(payload(responsePayload));
}
However, the endpoint I am testing is using basic authentication and it expects to read values in the Authorization header. It is not using spring-security for that task but it has custom logic that gets the HTTP headers by getting the HttpServletResponse from the TransportContextHolder. So, the request triggers the endpoint but it fails to retrieve the basic authentication base64 token.
The question is, how may I pass HTTP headers in that situation? Is it possible at all? If not what is the preferred alternative?
I have read the javadoc and I cannot find a way to pass the headers. Also, I have found this question which is similar but it doesn't help me much.

The difference between exchange method and execute method in spring rest template?

I have three questions!
First.
I am using the spring framework for sending the data through rest protocol.
restTemplate.exchange(requestUrl,HttpMethod.POST, request, listVo.getClass());
org.springframework.web.client.RestTemplate.exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<? extends Object> responseType, Object... uriVariables) throws RestClientException
I used it without any problem, but I want to know the purpose of the parameter, responseType.
The client don't use response data, but just use response status code / msg. So, I sent some meaningless
String data instead. But the error thrown that they accept "null". So I sent a "null" String. not null.
Then, the error got rid of. But there was another problem. Right after the client received the data from the server and paused for a long time. Then next line of codes are executed. What is problem?
Second
I can't find any references that use execute method of Spring RestTemplate.
Third
Like the title, What is the difference between the exchange method and the execute method in spring rest template?
Thanks for your time and effort.
Cheers.
exchange return type is ResponseEntity<T> while execute is T
Taken from "Pivotal Certified Spring Web Application Developer Exam" book
The execute and exchange methods can be used for any type of REST calls
The execute method can also be given a RequestCallback implementation as a parameter, which tells the RestTemplate what to do with the request before sending it to the server.

Ajaxupload and Spring MVC

If you upload a file via a normal form, then it works.
If you load the file / files with ajaxupload nothing works.
Error:
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
Code:
#RequestMapping (value = "/ upload", method = RequestMethod.POST)
public void upload (# RequestParam MultipartFile file,
HttpServletRequest request, HttpServletResponse response)
Purpose - Multibooting files with ajax, can anyone have a working example for the Spring.
I have a separate servlet that receives HttpServletRequest and parses everything is fine. On the client side ajaxupload.
If you try a simple Spring MVC transfer request in this class, he refuses to work, arguing that the request is not multipart. Spring is obtained as a sawing the original request?
Please change fileupload.js , search and comment out the line which has "application/octet-stream"
and add following line :
xhr.setRequestHeader("Content-Type", "multipart/form-data");

Spring MVC, how to have a controller handler handle all the requests before the mapped handlers?

I've wrote a web app with its brave controllers and handler mapping, everything with Spring 3.0 and controller annotations. Now turns out that I need simple and custom autentication. I don't want to use ACEGI for the moment, because I've no time to learn it. I'd like ideally that I could have a routine that gets called before every mapped handler, gets from the HttpSession the userId, checks if he is logged in and the session key and if not redirects to a login page. I've been thinking about an interceptor... the problem is that you have to use HandlerInterceptorAdapter, which has the following method:
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
that won't let me access the HttpSession associated with the request. How do I solve this?
Are you sure? You should be able to obtain the session through request.getSession().

Resources