我目前正在努力在某些条件下返回http响应状态代码。假设,taskService.getNewTasks的返回对象为null。在这种情况下,我想返回状态代码404.在某些例外情况下,我想返回50x,依此类推。
到目前为止我的代码
@RestController
public class TaskController {
@Autowired
private TaskService taskService;
@GetMapping(path = "gettasks")
private Future<Tasks> getNewTasks() {
return taskService.getNewTasks();
}
...
}
@Service
public class TaskService {
@Async
public Future<Tasks> getNewTasks() {
...
return CompletableFuture.completedFuture(tasks);
}
}
答案 0 :(得分:1)
如https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-return-types中所述,不支持@ResponseStatus
作为控制器处理程序方法的返回类型。
由于您使用的是objIE.Quit
,因此您可以返回春天支持的Private Function Mexico(partida As String) As String
partida = Left(partida, 8)
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "http://www.siicexcaaarem.org.mx/Bases/TIGIE2007.nsf/4caa80bd19d9258006256b050078593c/$searchForm?SearchView"
Cargar
objIE.document.getElementsByName("Query")(0).Value = partida
For Each boton In objIE.document.getElementsByTagName("input")
If boton.Value = "Search" Then
boton.Click
Exit For
End If
Next
Cargar
Application.Wait Now + TimeValue("00:00:03")
Dim temp As String
Dim i As Integer
For Each t In objIE.document.getElementsByTagName("tr")
If t.className = "domino-viewentry" Then
temp = t.Children(8).innerText
End If
Next
If InStr(temp, "*") > 0 Then
temp = Left(temp, Len(temp) - 1)
End If
If InStr(temp, "%") = 0 Then
temp = temp & "%"
End If
Mexico = temp
objIE.Quit
End Function
或Sub Mex()
MsgBox Mexico("33030001")
End Sub
。
如果完成时出现异常,则可以使用常规的Spring异常处理机制,例如使用objIE
注释异常。
答案 1 :(得分:0)
您可以使用ResponseEntity
来实现此目的。您无需更改前端。
@GetMapping(path = "gettasks")
private ResponseEntity<Future<Tasks>> getNewTasks(@PathVariable final String regidId) {
Future<Tasks> result = taskService.getNewTasks(regidId);
return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);//You can use HttpStatus.NOT_FOUND for 404
}
答案 2 :(得分:0)
这可能适合你。
@GetMapping(path = "gettasks")
private CompletableFuture<ResponseEntity<Tasks>> getNewTasks() {
CompletableFuture<Tasks> future = new CompletableFuture<>();
future.complete(taskService.getNewTasks());
if(yourCondition){
return future.thenApply(result -> new ResponseEntity<Tasks>(result, HttpStatus.STATUS_1));
}
return future.thenApply(result -> new ResponseEntity<Tasks>(result, HttpStatus.STATUS_2));
}