diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java index 301a5afd7c5..650e3c20b13 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,7 +60,7 @@ public class ExchangeResult { private static final Log logger = LogFactory.getLog(ExchangeResult.class); private static final List PRINTABLE_MEDIA_TYPES = Arrays.asList( - MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, + MediaType.parseMediaType("application/*+json"), MediaType.APPLICATION_XML, MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java index f09008c96fd..3d7c1361ed4 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseBodyResultHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -162,6 +162,7 @@ private HandlerResult getHandlerResult(Object controller, @Nullable Object retur return new HandlerResult(handlerMethod, returnValue, handlerMethod.getReturnType()); } + @SuppressWarnings("SameParameterValue") private void assertResponseBody(MockServerWebExchange exchange, @Nullable String responseBody) { StepVerifier.create(exchange.getResponse().getBody()) .consumeNextWith(buf -> assertThat(buf.toString(UTF_8)).isEqualTo(responseBody)) @@ -171,7 +172,7 @@ private void assertResponseBody(MockServerWebExchange exchange, @Nullable String @RestController - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "ConstantConditions"}) private static class TestRestController { public Mono handleToMonoVoid() { return null;} @@ -200,7 +201,7 @@ public ProblemDetail handleToProblemDetail() { @Controller - @SuppressWarnings("unused") + @SuppressWarnings({"unused", "ConstantConditions"}) private static class TestController { @ResponseBody diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java index 1a163a4c512..bff70afdd95 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -130,7 +131,7 @@ public void setup() throws Exception { @Test public void resolveArgumentParameterizedType() throws Exception { String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List> converters = new ArrayList<>(); @@ -150,7 +151,7 @@ public void resolveArgumentParameterizedType() throws Exception { public void resolveArgumentRawTypeFromParameterizedType() throws Exception { String content = "fruit=apple&vegetable=kale"; this.servletRequest.setMethod("GET"); - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE); List> converters = new ArrayList<>(); @@ -169,7 +170,7 @@ public void resolveArgumentRawTypeFromParameterizedType() throws Exception { @Test public void resolveArgumentClassJson() throws Exception { String content = "{\"name\" : \"Jad\"}"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType("application/json"); List> converters = new ArrayList<>(); @@ -186,7 +187,7 @@ public void resolveArgumentClassJson() throws Exception { @Test public void resolveArgumentClassString() throws Exception { String content = "foobarbaz"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType("application/json"); List> converters = new ArrayList<>(); @@ -201,7 +202,7 @@ public void resolveArgumentClassString() throws Exception { } @Test // SPR-9942 - public void resolveArgumentRequiredNoContent() throws Exception { + public void resolveArgumentRequiredNoContent() { this.servletRequest.setContent(new byte[0]); this.servletRequest.setContentType("text/plain"); List> converters = new ArrayList<>(); @@ -230,7 +231,7 @@ public void resolveArgumentTypeVariable() throws Exception { MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "{\"name\" : \"Jad\"}"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List> converters = new ArrayList<>(); @@ -250,7 +251,7 @@ public void resolveParameterizedWithTypeVariableArgument() throws Exception { MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "[{\"name\" : \"Jad\"}, {\"name\" : \"Robert\"}]"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List> converters = new ArrayList<>(); @@ -273,7 +274,7 @@ public void resolveArgumentTypeVariableWithNonGenericConverter() throws Exceptio MethodParameter methodParam = handlerMethod.getMethodParameters()[0]; String content = "{\"name\" : \"Jad\"}"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); List> converters = new ArrayList<>(); @@ -609,7 +610,7 @@ public void jacksonJsonViewWithResponseEntityAndXmlMessageConverter() throws Exc @Test // SPR-12501 public void resolveArgumentWithJacksonJsonView() throws Exception { String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class); @@ -634,7 +635,7 @@ public void resolveArgumentWithJacksonJsonView() throws Exception { @Test // SPR-12501 public void resolveHttpEntityArgumentWithJacksonJsonView() throws Exception { String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class); @@ -664,7 +665,7 @@ public void resolveArgumentWithJacksonJsonViewAndXmlMessageConverter() throws Ex "with" + "with" + "without"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE); Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class); @@ -692,7 +693,7 @@ public void resolveHttpEntityArgumentWithJacksonJsonViewAndXmlMessageConverter() "with" + "with" + "without"; - this.servletRequest.setContent(content.getBytes("UTF-8")); + this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_XML_VALUE); Method method = JacksonController.class.getMethod("handleHttpEntity", HttpEntity.class); @@ -774,7 +775,7 @@ public void jacksonSubTypeList() throws Exception { @Test // SPR-14520 public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { - this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); + this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class); @@ -794,7 +795,7 @@ public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { @Test // gh-24127 public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Exception { - this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); + this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8)); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE); Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class); @@ -836,6 +837,7 @@ private void assertContentDisposition(RequestResponseBodyMethodProcessor process } + @SuppressWarnings("ConstantConditions") String handle( @RequestBody List list, @RequestBody SimpleBean simpleBean, @@ -845,19 +847,23 @@ String handle( return null; } + @SuppressWarnings("ConstantConditions") Resource getImage() { return null; } + @SuppressWarnings("ConstantConditions") ProblemDetail handleAndReturnProblemDetail() { return null; } + @SuppressWarnings("ConstantConditions") @RequestMapping OutputStream handleAndReturnOutputStream() { return null; } + @SuppressWarnings("ConstantConditions") SimpleBean getSimpleBean() { return null; } @@ -895,7 +901,7 @@ private static class MySimpleParameterizedControllerWithList extends MyParameter } - @SuppressWarnings({ "serial" }) + @SuppressWarnings("NotNullFieldNotInitialized") private static class SimpleBean implements Identifiable { private Long id; @@ -922,7 +928,7 @@ public void setName(String name) { } - private final class ValidatingBinderFactory implements WebDataBinderFactory { + private static final class ValidatingBinderFactory implements WebDataBinderFactory { @Override public WebDataBinder createBinder(NativeWebRequest request, @Nullable Object target, String objectName) { @@ -943,6 +949,7 @@ public String handle() { return "hello"; } + @SuppressWarnings("ConstantConditions") @RequestMapping public CharSequence handleWithCharSequence() { return null; @@ -965,6 +972,7 @@ private interface MyJacksonView1 {} private interface MyJacksonView2 {} + @SuppressWarnings("NotNullFieldNotInitialized") private static class JacksonViewBean { @JsonView(MyJacksonView1.class) @@ -983,6 +991,7 @@ public void setWithView1(String withView1) { this.withView1 = withView1; } + @Nullable public String getWithView2() { return withView2; } @@ -991,6 +1000,7 @@ public void setWithView2(String withView2) { this.withView2 = withView2; } + @Nullable public String getWithoutView() { return withoutView; } @@ -1001,7 +1011,8 @@ public void setWithoutView(String withoutView) { } - @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") + @SuppressWarnings("NotNullFieldNotInitialized") + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") public static class ParentClass { private String parentProperty; @@ -1092,6 +1103,7 @@ public JacksonViewBean handleRequestBody(@JsonView(MyJacksonView1.class) @Reques return bean; } + @SuppressWarnings("ConstantConditions") @RequestMapping @ResponseBody public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity entity) {