Skip to content

Commit dc64fa2

Browse files
bottemavfabapp2
andauthored
Migrate JAX-RS Consumes and Produces annotations on class level (#872)
* Migrate JAX-RS Consumes and Produces annotations on class level * Additional tests --------- Co-authored-by: Fabian Krüger <[email protected]>
1 parent 264c7f9 commit dc64fa2

File tree

5 files changed

+138
-135
lines changed

5 files changed

+138
-135
lines changed

applications/spring-shell/src/test/java/org/springframework/sbm/recipes/MigrateJaxRsAnnotationsRecipeIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ protected String getTestSubDir() {
4444
import org.springframework.web.bind.annotation.PathVariable;
4545
import org.springframework.web.bind.annotation.RequestParam;
4646
47+
4748
@RestController
48-
@RequestMapping(value = "/")
49+
@RequestMapping(value = "/", produces = "application/json")
4950
public class PersonController {
5051
51-
@RequestMapping(value = "/json/{name}", produces = "application/json", consumes = "application/json", method = RequestMethod.POST)
52+
@RequestMapping(value = "/json/{name}", consumes = "application/json", method = RequestMethod.POST)
5253
public String getHelloWorldJSON(@PathVariable("name") String name) throws Exception {
5354
System.out.println("name: " + name);
5455
return "{\\"Hello\\":\\"" + name + "\\"";

applications/spring-shell/src/test/resources/testcode/bootify-jaxrs/src/main/java/com/example/jee/app/PersonController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
1010

1111
@Path("/")
12+
@Produces("application/json")
1213
public class PersonController {
1314

1415
@POST
1516
@Path("/json/{name}")
16-
@Produces("application/json")
1717
@Consumes("application/json")
1818
public String getHelloWorldJSON(@PathParam("name") String name) throws Exception {
1919
System.out.println("name: " + name);

components/sbm-recipes-jee-to-boot/src/main/java/org/springframework/sbm/jee/jaxrs/actions/ConvertJaxRsAnnotations.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,28 @@ private void transformTypeAnnotations(Type type) {
161161
if (found.isPresent()) {
162162
type.removeAnnotation(found.get());
163163
type.addAnnotation("org.springframework.web.bind.annotation.RestController");
164-
String rmAttrs = found.get().getAttributes().entrySet().stream().map(e -> e.getKey() + " = " + e.getValue().print()).collect(Collectors.joining(", "));
164+
Map<String, Expression> attributes = new LinkedHashMap<>(found.get().getAttributes());
165+
for (Annotation a : annotations) {
166+
if (a == null) {
167+
continue;
168+
}
169+
String fullyQualifiedName = a.getFullyQualifiedName();
170+
if (fullyQualifiedName != null) {
171+
switch (fullyQualifiedName) {
172+
case "javax.ws.rs.Consumes" -> {
173+
attributes.put("consumes", a.getAttribute("value"));
174+
type.removeAnnotation(a);
175+
}
176+
case "javax.ws.rs.Produces" -> {
177+
attributes.put("produces", a.getAttribute("value"));
178+
type.removeAnnotation(a);
179+
}
180+
default -> {
181+
}
182+
}
183+
}
184+
}
185+
String rmAttrs = attributes.entrySet().stream().map(e -> e.getKey() + " = " + e.getValue().print()).collect(Collectors.joining(", "));
165186
type.addAnnotation("@RequestMapping(" + rmAttrs + ")", "org.springframework.web.bind.annotation.RequestMapping");
166187
}
167188
}

components/sbm-recipes-jee-to-boot/src/test/java/org/springframework/sbm/jee/jaxrs/actions/MigrateJaxRsAnnotations_Test.java

Lines changed: 0 additions & 125 deletions
This file was deleted.
Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* @author Fabian Krüger
2929
*/
30-
public class ConvertJaxRsAnnotationsest {
30+
public class ConvertJaxRsAnnotationsTest {
3131

3232
private final static String SPRING_VERSION = "5.3.13";
3333

@@ -73,7 +73,8 @@ public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("ma
7373
.withJavaSources(restControllerCode)
7474
.withBuildFileHavingDependencies(
7575
"org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final",
76-
"org.springframework:spring-core:"+SPRING_VERSION
76+
"org.springframework:spring-core:"+SPRING_VERSION,
77+
"org.springframework:spring-web:"+SPRING_VERSION
7778
)
7879
.build();
7980

@@ -94,20 +95,20 @@ public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("ma
9495
import com.example.jeerest.Movie;
9596
import com.example.jeerest.MoviesBean;
9697
import org.springframework.beans.factory.annotation.Autowired;
98+
import org.springframework.web.bind.annotation.RequestMapping;
99+
import org.springframework.web.bind.annotation.RequestMethod;
97100
import org.springframework.web.bind.annotation.RestController;
98101
99102
import javax.ws.rs.DELETE;
100103
import javax.ws.rs.PUT;
101104
import javax.ws.rs.PathParam;
102-
import javax.ws.rs.Produces;
103105
import javax.ws.rs.QueryParam;
104106
import javax.ws.rs.core.MediaType;
105107
import java.util.List;
106108
107109
108-
@Produces({"application/json"})
109110
@RestController
110-
@RequestMapping(value = "movies")
111+
@RequestMapping(value = "movies", produces = {"application/json"})
111112
public class MoviesRest {
112113
113114
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@@ -119,15 +120,120 @@ public Movie find(@PathParam("id") Long id) {
119120
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
120121
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
121122
return service.getMovies(first, max, field, searchTerm);
122-
}
123+
}
123124
}
124125
""";
125126

126127
assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo(
127128
expected
128129
);
130+
}
131+
132+
@Test
133+
void classAnnotatedWithProducesAndConsumes() {
134+
@Language("java")
135+
String restControllerCode = """
136+
package com.example.jeerest.rest;
137+
138+
import javax.ws.rs.Consumes;
139+
import javax.ws.rs.Path;
140+
import javax.ws.rs.Produces;
141+
142+
@Path("movies")
143+
@Consumes("application/x-www-form-urlencoded")
144+
@Produces("application/json")
145+
public class MoviesRest {
146+
}
147+
""";
129148

149+
ProjectContext context = TestProjectContext.buildProjectContext()
150+
.withJavaSources(restControllerCode)
151+
.withBuildFileHavingDependencies(
152+
"org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final",
153+
"org.springframework:spring-core:"+SPRING_VERSION,
154+
"org.springframework:spring-web:"+SPRING_VERSION
155+
)
156+
.build();
130157

158+
ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations
159+
.builder()
160+
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())
161+
.description("Convert JAX-RS annotations into Spring Boot annotations.")
162+
.build();
163+
164+
convertJaxRsAnnotations.apply(context);
165+
166+
167+
@Language("java")
168+
String expected =
169+
"""
170+
package com.example.jeerest.rest;
171+
172+
import org.springframework.web.bind.annotation.RequestMapping;
173+
import org.springframework.web.bind.annotation.RestController;
174+
175+
176+
@RestController
177+
@RequestMapping(value = "movies", consumes = "application/x-www-form-urlencoded", produces = "application/json")
178+
public class MoviesRest {
179+
}
180+
""";
181+
182+
assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo(
183+
expected
184+
);
131185
}
132186

187+
@Test
188+
void classAnnotatedWithConsumes() {
189+
@Language("java")
190+
String restControllerCode = """
191+
package com.example.jeerest.rest;
192+
193+
import javax.ws.rs.Consumes;
194+
import javax.ws.rs.Path;
195+
196+
@Path("movies")
197+
@Consumes("application/x-www-form-urlencoded")
198+
public class MoviesRest {
199+
}
200+
""";
201+
202+
ProjectContext context = TestProjectContext.buildProjectContext()
203+
.withJavaSources(restControllerCode)
204+
.withBuildFileHavingDependencies(
205+
"org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final",
206+
"org.springframework:spring-core:"+SPRING_VERSION,
207+
"org.springframework:spring-web:"+SPRING_VERSION
208+
)
209+
.build();
210+
211+
ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations
212+
.builder()
213+
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())
214+
.description("Convert JAX-RS annotations into Spring Boot annotations.")
215+
.build();
216+
217+
convertJaxRsAnnotations.apply(context);
218+
219+
220+
@Language("java")
221+
String expected =
222+
"""
223+
package com.example.jeerest.rest;
224+
225+
import org.springframework.web.bind.annotation.RequestMapping;
226+
import org.springframework.web.bind.annotation.RestController;
227+
228+
229+
@RestController
230+
@RequestMapping(value = "movies", consumes = "application/x-www-form-urlencoded")
231+
public class MoviesRest {
232+
}
233+
""";
234+
235+
assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo(
236+
expected
237+
);
238+
}
133239
}

0 commit comments

Comments
 (0)