use of org.springframework.http.HttpStatus in project motan by weibocom.
the class CommandController method addCommand.
/**
* 向指定group添加指令
*
* @param group
* @param clientCommand
* @return
*/
@RequestMapping(value = "/{group}", method = RequestMethod.POST)
public ResponseEntity<Boolean> addCommand(@PathVariable("group") String group, @RequestBody ClientCommand clientCommand) {
if (StringUtils.isEmpty(group) || clientCommand == null) {
return new ResponseEntity<Boolean>(HttpStatus.BAD_REQUEST);
}
boolean result = commandService.addCommand(group, clientCommand);
HttpStatus status;
if (result) {
status = HttpStatus.OK;
} else {
status = HttpStatus.NOT_MODIFIED;
}
return new ResponseEntity<Boolean>(result, status);
}
use of org.springframework.http.HttpStatus in project spring-security by spring-projects.
the class BearerTokenServerAuthenticationEntryPoint method commence.
@Override
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException authException) {
return Mono.defer(() -> {
HttpStatus status = getStatus(authException);
Map<String, String> parameters = createParameters(authException);
String wwwAuthenticate = computeWWWAuthenticateHeaderValue(parameters);
ServerHttpResponse response = exchange.getResponse();
response.getHeaders().set(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate);
response.setStatusCode(status);
return response.setComplete();
});
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class ErrorHandlerIntegrationTests method emptyPathSegments.
// SPR-15560
@ParameterizedHttpServerTest
void emptyPathSegments(HttpServer httpServer) throws Exception {
startServer(httpServer);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
URI url = new URI("http://localhost:" + port + "//");
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
// Jetty 10+ rejects empty path segments, see https://github.com/eclipse/jetty.project/issues/6302,
// but an application can apply CompactPathRule via RewriteHandler:
// https://www.eclipse.org/jetty/documentation/jetty-11/programming_guide.php
HttpStatus expectedStatus = (httpServer instanceof JettyHttpServer ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
assertThat(response.getStatusCode()).isEqualTo(expectedStatus);
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class ViewResolutionResultHandlerTests method handleReturnValueTypes.
@Test
public void handleReturnValueTypes() {
Object returnValue;
MethodParameter returnType;
ViewResolver resolver = new TestViewResolver("account");
returnType = on(Handler.class).resolveReturnType(View.class);
returnValue = new TestView("account");
testHandle("/path", returnType, returnValue, "account: {id=123}");
returnType = on(Handler.class).resolveReturnType(Mono.class, View.class);
returnValue = Mono.just(new TestView("account"));
testHandle("/path", returnType, returnValue, "account: {id=123}");
returnType = on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class);
returnValue = "account";
testHandle("/path", returnType, returnValue, "account: {id=123}", resolver);
returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(String.class);
returnValue = "123";
testHandle("/account", returnType, returnValue, "account: {id=123, myString=123}", resolver);
returnType = on(Handler.class).resolveReturnType(Mono.class, String.class);
returnValue = Mono.just("account");
testHandle("/path", returnType, returnValue, "account: {id=123}", resolver);
returnType = on(Handler.class).resolveReturnType(Model.class);
returnValue = new ConcurrentModel().addAttribute("name", "Joe").addAttribute("ignore", null);
testHandle("/account", returnType, returnValue, "account: {id=123, name=Joe}", resolver);
// Work around caching issue...
ResolvableType.clearCache();
returnType = on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(Map.class);
returnValue = Collections.singletonMap("name", "Joe");
testHandle("/account", returnType, returnValue, "account: {id=123, name=Joe}", resolver);
// Work around caching issue...
ResolvableType.clearCache();
returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(Map.class);
returnValue = Collections.singletonMap("name", "Joe");
testHandle("/account", returnType, returnValue, "account: {id=123, myMap={name=Joe}}", resolver);
returnType = on(Handler.class).resolveReturnType(TestBean.class);
returnValue = new TestBean("Joe");
String responseBody = "account: {id=123, " + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]}";
testHandle("/account", returnType, returnValue, responseBody, resolver);
returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(Long.class);
testHandle("/account", returnType, 99L, "account: {id=123, myLong=99}", resolver);
returnType = on(Handler.class).resolveReturnType(Rendering.class);
HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
returnValue = Rendering.view("account").modelAttribute("a", "a1").status(status).header("h", "h1").build();
String expected = "account: {a=a1, id=123}";
ServerWebExchange exchange = testHandle("/path", returnType, returnValue, expected, resolver);
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(status);
assertThat(exchange.getResponse().getHeaders().getFirst("h")).isEqualTo("h1");
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class RouterFunctionBuilderTests method route.
@Test
public void route() {
RouterFunction<ServerResponse> route = RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().build()).POST("/", RequestPredicates.contentType(MediaType.TEXT_PLAIN), request -> ServerResponse.noContent().build()).route(HEAD("/foo"), request -> ServerResponse.accepted().build()).build();
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/foo").build();
ServerRequest getRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<Integer> responseMono = route.route(getRequest).flatMap(handlerFunction -> handlerFunction.handle(getRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).expectNext(200).verifyComplete();
mockRequest = MockServerHttpRequest.head("https://example.com/foo").build();
ServerRequest headRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
responseMono = route.route(headRequest).flatMap(handlerFunction -> handlerFunction.handle(headRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).expectNext(202).verifyComplete();
mockRequest = MockServerHttpRequest.post("https://example.com/").contentType(MediaType.TEXT_PLAIN).build();
ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
responseMono = route.route(barRequest).flatMap(handlerFunction -> handlerFunction.handle(barRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).expectNext(204).verifyComplete();
mockRequest = MockServerHttpRequest.post("https://example.com/").build();
ServerRequest invalidRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
responseMono = route.route(invalidRequest).flatMap(handlerFunction -> handlerFunction.handle(invalidRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(responseMono).verifyComplete();
}
Aggregations