I'm trying to have a @RestController which takes a @PathVariable return a specific object in JSON format, along with proper status code. So far the way the code is, it will return the object in JSON format because it is using Spring 4 built in Jackson library by default.
However I do not know how to make it so it will give a message to the user saying we want an api variable, then JSON data, then Error code (Or success code depending if all went well). Example output would be:
Please enter api value as parameter (NOTE this can be in JSON as well if needed)
{"id": 2, "api": "3000105000" ... } (NOTE this will be the JSON response object)
Status Code 400 (OR proper status code)
The url with parameter look like this
http://localhost:8080/gotech/api/v1/api/3000105000 The code I have so far:
@RestController @RequestMapping(value = "/api/v1") public class ClientFetchWellDataController { @Autowired private OngardWellService ongardWellService; @RequestMapping(value = "/wells/{apiValue}", method = RequestMethod.GET) @ResponseBody public OngardWell fetchWellData(@PathVariable String apiValue){ try{ OngardWell ongardWell = new OngardWell(); ongardWell = ongardWellService.fetchOneByApi(apiValue); return ongardWell; }catch(Exception ex){ String errorMessage; errorMessage = ex + " <== error"; return null; } } }
/api/v1/wells/{apiValue}, not/api/v1/api/{apiValue}