Hi, how can you control the http responses? Exampl...
# server
e
Hi, how can you control the http responses? Example If it is a 400 or 500. Return a custom message
a
in what framework?
e
Spring
a
Copy code
@RequestMapping(value = "/wells/{apiValue}", method = RequestMethod.GET)
public ResponseEntity<?> fetchWellData(@PathVariable String apiValue){
    try{
        OngardWell ongardWell = new OngardWell();
        ongardWell = ongardWellService.fetchOneByApi(apiValue);

        return new ResponseEntity<>(ongardWell, HttpStatus.OK);
    }catch(Exception ex){
        String errorMessage;
        errorMessage = ex + " <== error";
        return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
    }
}
😲 1
@RestController
 is not appropriate for this. If you need to return different types of responses, use a 
ResponseEntity<?>
 where you can explicitly set the status code. The 
body
 of the 
ResponseEntity
 will be handled the same way as the return value of any 
@ResponseBody
 annotated method.
e
Wow
Yeah. I used Response Entity, but my answer it's just ResponseEntity.ok(response)
a
can you paste your code snippet
e
Yeap
I'll do it
Copy code
@RestController
@RequestMapping(value = [(Route.QUERIES)])
@Api(value = "DataQueryBuro", description = "API QUERY - V1", tags = ["DataQueryBuro"])
class BuroDataController : BuroDataInterface {

    private val logger = LoggerFactory.getLogger(BuroDataController::class.java)

    @Autowired
    private lateinit var buroDataService: BuroDataService

    @PostMapping
    override fun getDataQueryBuro(@Valid @RequestBody body: BuroDataRequest): ResponseEntity<Any> {
        <http://logger.info|logger.info>("--RPP-BURO-CREDITO-MS --getDataConsultationBuro --received request [{}]", body)
        val buroResponse = buroDataService.getData(body)
        return ResponseEntity.ok(buroResponse.toBuroDataCustomResponse()!!)
    }

}
this is th way in case all data is donw
done
when got a wrong values , mi server responses 500 internal error
t
Sounds like an unhandled exception is being thrown. Might be in your request binding, or in your buroDataService. I wouldn't use a
!!
, if that is nullable you need to properly handle it. I'd also double check your javax.validation constraints on your BuroDataRequest to make sure you are checking all your required fields. The easiest way to troubleshoot this is to read your logs and see what is making the request fail.
e
Thanks Tom Hermann