當前位置: 首頁>>代碼示例>>Java>>正文


Java ServerHttpResponse.setStatusCode方法代碼示例

本文整理匯總了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);
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:23,代碼來源:AbstractSockJsService.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:HtmlFileTransportHandler.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:JsonpPollingTransportHandler.java

示例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;
}
 
開發者ID:metasfresh,項目名稱:metasfresh-webui-api,代碼行數:24,代碼來源:WebSocketConfig.java

示例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();

}
 
開發者ID:KonkerLabs,項目名稱:konker-platform,代碼行數:23,代碼來源:CrudResponseAdvice.java

示例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("係統異常");
    }
}
 
開發者ID:FastBootWeixin,項目名稱:FastBootWeixin,代碼行數:16,代碼來源:WxMediaResponseBodyAdvice.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:14,代碼來源:RequestMappingHandlerAdapterTests.java

示例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);
}
 
開發者ID:philwebb,項目名稱:spring-boot-concourse,代碼行數:9,代碼來源:Dispatcher.java

示例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));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:8,代碼來源:AbstractHandshakeHandler.java

示例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));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:8,代碼來源:AbstractHandshakeHandler.java

示例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()));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:11,代碼來源:AbstractHandshakeHandler.java

示例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);
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:10,代碼來源:AbstractHttpReceivingTransportHandler.java

示例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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:42,代碼來源:ResponseBodyEmitterReturnValueHandler.java


注:本文中的org.springframework.http.server.ServerHttpResponse.setStatusCode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。