我正在try 使用@RestController
,它以@PathVariable
返回JSON格式的特定对象,以及正确的状态代码.到目前为止,代码将以JSON格式返回对象,因为默认情况下,它使用的是Jackson库中内置的Spring 4.
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:
请输入api值作为参数(注意,如果需要,也可以使用JSON)
{"id":2,"api":"3000105000"…}(注意这将是JSON响应对象)
Status Code 400 (OR proper status code)
The url with parameter look like this
http://localhost:8080/gotech/api/v1/api/3000105000
到目前为止,我掌握的代码如下:
@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;
}
}
}