logo
down
shadow

Spring-MVC Exception handler returns OK when writing into response


Spring-MVC Exception handler returns OK when writing into response

By : Tom McGahan
Date : November 25 2020, 07:27 PM
I think the issue was by ths following , The reason is because you are explicitly writing to the output stream, instead of letting the framework handle it. The header has to go before the body content is written, if you are explicitly handling writing to the output stream, you will have to write the header also yourself.
To let the framework handle the entire flow, you can instead do this:
code :


Share : facebook icon twitter icon
How to handle HttpMediaTypeNotAcceptableException by writing error content to the response body using exception handler

How to handle HttpMediaTypeNotAcceptableException by writing error content to the response body using exception handler


By : Daniela Lunguleasa
Date : March 29 2020, 07:55 AM
Any of those help Resolved by setting different content negotiation strategy FixedContentNegotiationStrategy for ExceptionHandlerExceptionResolver and HeaderContentNegotiationStrategy for RequestResponseBodyMethodProcessor.
Returning response entity from an Exception handler [Spring]

Returning response entity from an Exception handler [Spring]


By : englandp2736
Date : March 29 2020, 07:55 AM
hop of those help? I use the following code to handle all exceptions of type RuntimeException in class annotated with @ControllerAdvice , A quick fix if you require a returned JSON format:
code :
@ExceptionHandler(RuntimeException.class)
 public ResponseEntity<String> RuntimeExceptionHandler(RuntimeException e) {
  JSONObject response = new JSONObject();
  response.put("message", e.getMessage());
  return new ResponseEntity<String>(response.toString(), HttpStatus.BAD_REQUEST);
 }
Spring - Intercept response from another bean by exception handler

Spring - Intercept response from another bean by exception handler


By : Daniel Phillips
Date : March 29 2020, 07:55 AM
may help you . It is not possible to get control back to controller from exception handler which is invoke after handling its method. Your current flow looks like this call A.addPersons -> invoke B.addPerson -> B throws exception -> exception is propagate to A controller and it's save as dispatchException to futher processing in DispatcherServlet after handling controller method (not ResponseEntity with status 400) -> exception is handled with MyExceptionHandler. From this place you cannot back to controller.
I'm not sure what you want to do it with this exception in controller but solution may look like this:
code :
@RestController
@RequestMapping("/resources")
public class AController {

    private BService service;

    @Autowired
    public AController(BService service) {
        this.service = service;
    }

    @RequestMapping("/test")
    public ResponseEntity<String> test() {
        ResponseEntity<String> result = service.test();
        if (result.hasBody()) {
            //doSomething
        }

        return result; //or list like you did
    }
}

@Service
public class BService {

    public ResponseEntity<String> test() {
        try {
            return ResponseEntity.status(201).body(getResponse()); //this always throws exception. It's just for purpose of example
        } catch (CustomException ex) {
            return ResponseEntity.status(400).build();
        }

    }

    private String getResponse() {
        throw new CustomException("Not OK!");
    }
}
Is it good practice to have Generic Exception class handler in global exception handler with spring rest?

Is it good practice to have Generic Exception class handler in global exception handler with spring rest?


By : Ross Dooneyu
Date : March 29 2020, 07:55 AM
like below fixes the issue
Should I replace it with RunTimeException as this block is for exception occurred at runtime?
code :
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<Foo> getOne(@PathVariable long id) throws Exception {
       // ....           
}
Spring Exception Handler returns partial response - maybe bug?

Spring Exception Handler returns partial response - maybe bug?


By : user1938622
Date : March 29 2020, 07:55 AM
should help you out Once the HTTP response has been committed, there's no way to change the HTTP status or headers. At that point, that response might be sitting in network buffers or even been sent already to the client. At that point, there's no way to take it back.
So in this case, Spring cannot change the response.
Related Posts Related Posts :
  • Autowired not happening for bean with @Async method
  • Spring Framework does not required sever. Is it true?
  • How to implement Generics DAO with Spring JDBC template
  • Escaping a | (pipe) in SpEL
  • Spring: Using @Qualifier with Property Placeholder
  • abount the singleton beans of spring
  • Spring @Autowired Not Working Error Creating Bean Injection Of Autowire dependencies failed
  • My applicationContext.xml is not valid when i deploy my project on tomcat7:org.springframework.beans.factory.parsing.Bea
  • Storing trailing zeroes in database with JPA and Oracle
  • Making specific method non transactional in Spring
  • Spring more than one profile and ${spring.profiles.active}
  • Spring Batch - interrupt thread when job stops
  • Can I use spring AOP to log start and end of a method to help easy debuging
  • How to use different error codes for validation messages?
  • Spring Data Rest and collections with unique constraints
  • Thymeleaf + Spring Checkboxes not binding
  • Spring Boot: Publish Thymeleaf template without restarting the server
  • How long will Spring 3.x continue to be supported?
  • Spring Security without a login form
  • SOAP UI not finding WSDL depending XSD files in Spring WS Soap Service
  • Spring MVC- Joda datetime error message for invalid date
  • Broadleaf Demosite on JBOSS 7
  • Spring Mongo mapping variable data
  • MySQL and Infinispan - JTA implementation
  • spring boot autoconfiguration with jdbc template autowiring dataSource issue
  • Hibernate and @SessionAttribute
  • DefaultMessageListenerContainer threading issue
  • Good sample spring and hibernate web application book?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org