除了200之外,我如何在nestjs中发送错误代码?我尝试在方法中注入响应对象,但没有方法发送错误。
save(@Body() body: any, @Res() response: Response): string {
console.log("posting...")
console.log(body)
return "saving " + JSON.stringify(body)
}
Run Code Online (Sandbox Code Playgroud)
上面的代码发送带有 20X 状态的正文,我想发送不同的状态代码,例如 400 或 500。
我写了以下控制器方法:
@RequestMapping(value = "/member/createCompany/uploadImage1", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public Long handle() {
return 1l;
}
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中请求此方法时,我看到以下内容:
HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]
type Exception report
message Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown return value type [java.lang.Long]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) …Run Code Online (Sandbox Code Playgroud) 如何在Spring MVC请求处理程序中方便地有条件地设置HTTP状态代码?
我有一个响应POST请求的请求处理程序,用于创建新资源.如果请求有效,我希望它重定向到新资源的URI,返回201(已创建)HTTP状态代码.如果请求无效,我希望它能够让用户有机会更正提交表单中的错误,并且不应该提供201的状态代码.
@RequestMapping(value = { "/myURI/" }, method = RequestMethod.POST)
public String processNewThingForm(
@ModelAttribute(value = "name") final String name,
final BindingResult bindingResult) {
myValidator.validate(name, bindingResult);
if (!bindingResult.hasErrors()) {
getService().createThing(name);
return "redirect:" + name;
} else {
return "newThingView";
}
Run Code Online (Sandbox Code Playgroud)
}
但这并没有为重定向案例提供正确的响应状态.
我不能简单地添加一个@ResponseStatus,因为有两种可能的状态.我希望有一种比手动操作HttpServletResponse更简洁的方法.我想指出要使用的视图名称,所以我不能让请求处理程序返回一个ResponseEntity状态设置正确的对象.
我有一个像下面这样的控制器
@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)
@ResponseBody
public String handleIOException(Throwable ex) {
ErrorResponse errorResponse = errorHandler.handelErrorResponse(ex);
return errorResponse.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
控制器有一个错误处理机制,在错误处理选项中它总是返回期望失败状态代码417.但我需要根据错误类型设置动态错误Http状态代码,如500,403等.我该怎么做呢?
如果用户没有输入我编码的两个名称,如何从Spring MVC更改/更新以下REST调用以返回错误..类似于未找到的东西?
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
@ResponseBody
public User getName(@PathVariable String name, ModelMap model)
{
logger.debug("I am in the controller and got user name: " + name);
/*
Simulate a successful lookup for 2 users, this is where your real lookup code would go
*/
if ("name2".equals(name))
{
return new User("real name 2", name);
}
if ("name1".equals(name))
{
return new User("real name 1", name);
}
return null;
}
Run Code Online (Sandbox Code Playgroud) spring-mvc ×4
java ×2
javascript ×2
rest ×2
spring ×2
http ×1
json ×1
nestjs ×1
typescript ×1
web-services ×1