How to handle Internal server error (500) on spring rest API?

@ControllerAdvice public class Handler { @ExceptionHandler(Exception.class) public ResponseEntity<Object> handle(Exception ex, HttpServletRequest request, HttpServletResponse response) { if (ex instanceof NullPointerException) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } ExceptionHandler Documenation – here you can find all objects the method signature can operate with. ControllerAdvice – with no additional properties it will handle all exceptions, so …

Read more

Is there a method built in spring MockMVC to get json content as Object?

As far as I know MockHttpServletResponse (Unlike RestTemplate) doesn’t have any method which could convert returned JSON to a particular type. So what you could do is use Jackson ObjectMapper to convert JSON string to a particular type Something like this String json = rt.getResponse().getContentAsString(); SomeClass someClass = new ObjectMapper().readValue(json, SomeClass.class); This will give you …

Read more

Is synchronization within an HttpSession feasible?

I found this nice explanation in spring-mvc JavaDoc for WebUtils.getSessionMutex(): In many cases, the HttpSession reference itself is a safe mutex as well, since it will always be the same object reference for the same active logical session. However, this is not guaranteed across different servlet containers; the only 100% safe way is a session …

Read more

How can I have case insensitive URLS in Spring MVC with annotated mappings

Spring 4.2 will support case-insensitive path matching. You can configure it as follows: @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configurePathMatch(PathMatchConfigurer configurer) { AntPathMatcher matcher = new AntPathMatcher(); matcher.setCaseSensitive(false); configurer.setPathMatcher(matcher); } }

Forward HttpServletRequest to a different server

Discussions of whether you should do forwarding this way aside, here’s how I did it: package com.example.servlets; import java.net.HttpURLConnection; import java.net.URL; import java.util.Enumeration; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.example.servlets.GlobalConstants; @SuppressWarnings(“serial”) public class ForwardServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) { forwardRequest(“GET”, req, resp); } @Override public void doPost(HttpServletRequest req, …

Read more

No adapter for handler exception

By default the spring mvc defines 3 different request handler adapters, they are org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter So you need not have to define them in your context file, but if you define at least one handler adapter in your context files, spring will not create the default adapters. In your configuraion you are using <mvc:annotation-driven …

Read more