本文整理汇总了Java中org.springframework.http.server.ServerHttpResponse.setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:Java ServerHttpResponse.setStatusCode方法的具体用法?Java ServerHttpResponse.setStatusCode怎么用?Java ServerHttpResponse.setStatusCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.http.server.ServerHttpResponse
的用法示例。
在下文中一共展示了ServerHttpResponse.setStatusCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public void handle(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
if (HttpMethod.GET == request.getMethod()) {
addNoCacheHeaders(response);
if (checkOrigin(request, response)) {
response.getHeaders().setContentType(new MediaType("application", "json", UTF8_CHARSET));
String content = String.format(
INFO_CONTENT, random.nextInt(), isSessionCookieNeeded(), isWebSocketEnabled());
response.getBody().write(content.getBytes());
}
}
else if (HttpMethod.OPTIONS == request.getMethod()) {
if (checkOrigin(request, response)) {
addCacheHeaders(response);
response.setStatusCode(HttpStatus.NO_CONTENT);
}
}
else {
sendMethodNotAllowed(response, HttpMethod.OPTIONS, HttpMethod.GET);
}
}
示例2: handleRequestInternal
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
AbstractHttpSockJsSession sockJsSession) throws SockJsException {
String callback = getCallbackParam(request);
if (!StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
try {
response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
}
catch (IOException ex) {
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), ex);
}
return;
}
super.handleRequestInternal(request, response, sockJsSession);
}
示例3: handleRequestInternal
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
AbstractHttpSockJsSession sockJsSession) throws SockJsException {
try {
String callback = getCallbackParam(request);
if (!StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
return;
}
}
catch (Throwable ex) {
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
throw new SockJsTransportFailureException("Failed to send error", sockJsSession.getId(), ex);
}
super.handleRequestInternal(request, response, sockJsSession);
}
示例4: beforeHandshake
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public boolean beforeHandshake(final ServerHttpRequest request, final ServerHttpResponse response, final WebSocketHandler wsHandler, final Map<String, Object> attributes) throws Exception
{
// UserSession.getCurrent().assertLoggedIn();
// return true;
final UserSession userSession = UserSession.getCurrentOrNull();
if (userSession == null)
{
logger.warn("Websocket connection not allowed (missing userSession)");
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return false;
}
if (!userSession.isLoggedIn())
{
logger.warn("Websocket connection not allowed (not logged in) - userSession={}", userSession);
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return false;
}
return true;
}
示例5: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
HttpStatus httpStatus = HttpStatus.OK;
if (request.getMethod().equals(HttpMethod.DELETE)) {
httpStatus = HttpStatus.NO_CONTENT;
} else if (request.getMethod().equals(HttpMethod.POST)) {
httpStatus = HttpStatus.CREATED;
} else if (request.getMethod().equals(HttpMethod.PUT)) {
httpStatus = HttpStatus.OK;
} else if (request.getMethod().equals(HttpMethod.GET)) {
if (body == null) {
httpStatus = HttpStatus.NOT_FOUND;
}
}
response.setStatusCode(httpStatus);
return RestResponseBuilder.ok().withHttpStatus(httpStatus).withResult(body).getResponse();
}
示例6: handle
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
/**
* Handle a server request.
* @param request the request
* @param response the response
* @throws IOException in case of I/O errors
*/
public void handle(ServerHttpRequest request, ServerHttpResponse response)
throws IOException {
try {
Assert.state(request.getHeaders().getContentLength() > 0, "No content");
ObjectInputStream objectInputStream = new ObjectInputStream(
request.getBody());
ClassLoaderFiles files = (ClassLoaderFiles) objectInputStream.readObject();
objectInputStream.close();
this.server.updateAndRestart(files);
response.setStatusCode(HttpStatus.OK);
}
catch (Exception ex) {
logger.warn("Unable to handler restart server HTTP request", ex);
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:23,代码来源:HttpRestartServer.java
示例7: beforeBodyWrite
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public WxMediaResource beforeBodyWrite(WxMediaResource body, MethodParameter returnType,
MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
if (body == null || !body.isUrlMedia()) {
return body;
}
try {
response.getHeaders().setLocation(body.getURI());
response.setStatusCode(HttpStatus.FOUND);
return null;
} catch (IOException e) {
throw new WxAppException("系统异常");
}
}
示例8: handleReturnValue
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) {
ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
outputMessage.setStatusCode(responseEntity.getStatusCode());
outputMessage.getHeaders().putAll(responseEntity.getHeaders());
returnValue = responseEntity.getBody();
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
}
ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
ShallowEtagHeaderFilter.disableContentCaching(request);
Assert.isInstanceOf(StreamingResponseBody.class, returnValue);
StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue;
Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody);
WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:34,代码来源:StreamingResponseBodyReturnValueHandler.java
示例9: beforeBodyWriteInternal
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
response.setStatusCode(HttpStatus.OK);
Map<String, Object> map = new LinkedHashMap<>();
map.put("status", status);
map.put("message", bodyContainer.getValue());
bodyContainer.setValue(map);
}
示例10: handle
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
private void handle(Handler handler, ServerHttpRequest request,
ServerHttpResponse response) throws IOException {
if (!this.accessManager.isAllowed(request)) {
response.setStatusCode(HttpStatus.FORBIDDEN);
return;
}
handler.handle(request, response);
}
示例11: handleInvalidUpgradeHeader
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
if (logger.isErrorEnabled()) {
logger.error("Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade());
}
response.setStatusCode(HttpStatus.BAD_REQUEST);
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes(UTF8_CHARSET));
}
示例12: handleInvalidConnectHeader
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
if (logger.isErrorEnabled()) {
logger.error("Handshake failed due to invalid Connection header " + request.getHeaders().getConnection());
}
response.setStatusCode(HttpStatus.BAD_REQUEST);
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes(UTF8_CHARSET));
}
示例13: handleWebSocketVersionNotSupported
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {
if (logger.isErrorEnabled()) {
String version = request.getHeaders().getFirst("Sec-WebSocket-Version");
logger.error("Handshake failed due to unsupported WebSocket version: " + version +
". Supported versions: " + Arrays.toString(getSupportedVersions()));
}
response.setStatusCode(HttpStatus.UPGRADE_REQUIRED);
response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION,
StringUtils.arrayToCommaDelimitedString(getSupportedVersions()));
}
示例14: handleReadError
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
private void handleReadError(ServerHttpResponse response, String error, String sessionId) {
try {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
response.getBody().write(error.getBytes(UTF8_CHARSET));
}
catch (IOException ex) {
throw new SockJsException("Failed to send error: " + error, sessionId, ex);
}
}
示例15: handleReturnValue
import org.springframework.http.server.ServerHttpResponse; //导入方法依赖的package包/类
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) {
ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
outputMessage.setStatusCode(responseEntity.getStatusCode());
outputMessage.getHeaders().putAll(responseEntity.getHeaders());
returnValue = responseEntity.getBody();
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
}
ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
ShallowEtagHeaderFilter.disableContentCaching(request);
Assert.isInstanceOf(ResponseBodyEmitter.class, returnValue);
ResponseBodyEmitter emitter = (ResponseBodyEmitter) returnValue;
emitter.extendResponse(outputMessage);
// Commit the response and wrap to ignore further header changes
outputMessage.getBody();
outputMessage.flush();
outputMessage = new StreamingServletServerHttpResponse(outputMessage);
DeferredResult<?> deferredResult = new DeferredResult<Object>(emitter.getTimeout());
WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
emitter.initialize(handler);
}