codecamp

Micronaut 响应状态

默认情况下,Micronaut 控制器操作会使用 200 HTTP 状态代码进行响应。

如果操作返回 HttpResponse,请使用 status 方法配置响应的状态代码。

 Java Groovy  Kotlin 
@Get(value = "/http-response", produces = MediaType.TEXT_PLAIN)
public HttpResponse httpResponse() {
    return HttpResponse.status(HttpStatus.CREATED).body("success");
}
@Get(value = "/http-response", produces = MediaType.TEXT_PLAIN)
HttpResponse httpResponse() {
    HttpResponse.status(HttpStatus.CREATED).body("success")
}
@Get(value = "/http-response", produces = [MediaType.TEXT_PLAIN])
fun httpResponse(): HttpResponse<String> {
    return HttpResponse.status<String>(HttpStatus.CREATED).body("success")
}

您还可以使用@Status 注释。

 Java Groovy  Kotlin 
@Status(HttpStatus.CREATED)
@Get(produces = MediaType.TEXT_PLAIN)
public String index() {
    return "success";
}
@Status(HttpStatus.CREATED)
@Get(produces = MediaType.TEXT_PLAIN)
String index() {
    "success"
}
@Status(HttpStatus.CREATED)
@Get(produces = [MediaType.TEXT_PLAIN])
fun index(): String {
    return "success"
}

甚至用 HttpStatus 回应

 Java Groovy  Kotlin 
@Get("/http-status")
public HttpStatus httpStatus() {
    return HttpStatus.CREATED;
}
@Get("/http-status")
HttpStatus httpStatus() {
    HttpStatus.CREATED
}
@Get("/http-status")
fun httpStatus(): HttpStatus {
    return HttpStatus.CREATED
}


Micronaut HttpRequest 和 HttpResponse
Micronaut 响应 Content-Type
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Micronaut 独立命令行应用程序

Micronaut 安全

Micronaut 多租户

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }