Programmatically change http response status using spring 3 restful

前端 未结 4 1288
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 15:46

I have a controller like below

@Controller(\"myController\")
@RequestMapping(\"api\")
public class MyController {

     @RequestMapping(method = RequestMetho         


        
4条回答
  •  长情又很酷
    2021-01-12 16:46

    I get a solution and going to share this and also like to know any good suggestions.

    @Controller("myController")
    @RequestMapping("api")
    public class MyController {
    
        @RequestMapping(method = RequestMethod.GET, value = "/get/info/{id}", headers = "Accept=application/json")
        public @ResponseBody
        Student getInfo(@PathVariable String info) {
            // ...
        }
    
    }
    
    
    // ...    
        @ExceptionHandler(Throwable.class)
        //@ResponseStatus( HttpStatus.EXPECTATION_FAILED)<

    Expected out in rest client :

    502 Bad Gateway
    {
        "status":"BAD_GATEWAY",
        "error":"java.lang.UnsupportedOperationException",
        "message":"Some error message"
    }
    

    Thanks for your replies. I still need pointers for good practices.

提交回复
热议问题