2

Im trying to deserialize a JSON object coming from a Javascript UI, but I keep getting a 400 - "The request sent by the client was syntactically incorrect" error. Note, I know the REST service works because I can hit the service successfully if I hard code the JSON, but now I'm trying to generate the JSON from an actual JS object.

Does Spring generate log messages that can help with this? If so, then how do I set this up?

Mr Smith
  • 3,318
  • 9
  • 47
  • 85
  • i think you have to correct your json. use online json editor: jsonlintpro – Kumaresan Perumal Nov 17 '15 at 19:17
  • 1
    You can add a servlet filter that prints the request, with spring you could base it on https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/AbstractRequestLoggingFilter.html See http://stackoverflow.com/q/12371168/995891 for example – zapl Nov 17 '15 at 19:23
  • Can you post the JSON and code that handles it? How can we say what is wrong without seeing anything? – minion Nov 17 '15 at 20:06
  • I found the issue, the case was wrong on one class property. but it would still be nice to log messages from @RequestMapping. Is checking Fiddler ( or another HTTP utility) my best bet? – Mr Smith Nov 17 '15 at 20:11
  • Fiddler or similar is something you should definitely have in your toolbelt. Usability wise much nicer than logging for example. – zapl Nov 17 '15 at 20:25

1 Answers1

6

You can add your own logging filter to the processing chain. With spring boot simply by providing the following for example:

import javax.servlet.Filter;
...

@Configuration / @SpringBootApplication
class Something {

    @Bean
    public Filter loggingFilter(){
        return new AbstractRequestLoggingFilter() {
            private final Logger log = LoggerFactory
                    .getLogger(Something.class);

            {
                setIncludeClientInfo(true);
                setIncludeQueryString(true);
                setIncludePayload(true);
            }

            @Override
            protected void beforeRequest(HttpServletRequest request, String message) {
                // not needed
            }

            @Override
            protected void afterRequest(HttpServletRequest request, String message) {
                log.info(message);
            }
        };
    }

results in something similar to

{timestamp and such} : After request [uri=/v1/thing/1007?null;client=127.0.0.1;payload=  {
    "name": "test7"
  ]

with

   {
        "name": "test7"

being a malformed body. This isn't the nicest of filters since it omits useful information such as the http method (POST, GET, ..) the time it took for the request, the response, ... But if you look at it's source you can easily write a nicer one that fits your needs. Especially since ContentCachingRequestWrapper and ContentCachingResponseWrapper allow you to get a copy of request and response very easily.

Besides servlet filters you can also add a HandlerInterceptor like https://stackoverflow.com/a/28022330 - There you have access to SpringMVC specific details but you can't access request & response content without consuming them (compare https://stackoverflow.com/a/2171633 )

Community
  • 1
  • 1
zapl
  • 63,179
  • 10
  • 123
  • 154