Skip to content

Commit 8a44038

Browse files
authored
Merge branch 'main' into 400-add-action-to-set-unitname-attribute-on-persistencecontext-to-default
2 parents 21864f4 + 4320779 commit 8a44038

File tree

2 files changed

+100
-4
lines changed

2 files changed

+100
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private void transformMethodAnnotations(Type type) {
5555
.forEach(this::convertJaxRsMethodToSpringMvc);
5656
}
5757

58-
private void convertJaxRsMethodToSpringMvc(Method method) {
58+
void convertJaxRsMethodToSpringMvc(Method method) {
5959
Map<String, Expression> attrs = new LinkedHashMap<>();
6060
Set<String> methods = new LinkedHashSet<>();
6161
var annotations = method.getAnnotations();
@@ -127,11 +127,13 @@ private void convertJaxRsMethodToSpringMvc(Method method) {
127127
}
128128

129129
sb.append(attrs.entrySet().stream()
130-
.map(e -> e.getKey() + " = " + e.getValue().print())
131-
.collect(Collectors.joining(", ")));
130+
.map(e -> e.getKey() + " = " + e.getValue().print())
131+
.collect(Collectors.joining(", ")));
132132

133133
if (!methods.isEmpty()) {
134-
sb.append(", ");
134+
if(!attrs.entrySet().isEmpty()) {
135+
sb.append(", ");
136+
}
135137
if (methods.size() == 1) {
136138
sb.append("method = RequestMethod." + methods.iterator().next());
137139
} else {

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,109 @@
1515
*/
1616
package org.springframework.sbm.jee.jaxrs.actions;
1717

18+
import org.intellij.lang.annotations.Language;
1819
import org.springframework.sbm.java.api.JavaSource;
1920
import org.springframework.sbm.engine.context.ProjectContext;
21+
import org.springframework.sbm.java.api.Method;
22+
import org.springframework.sbm.java.migration.conditions.HasTypeAnnotation;
2023
import org.springframework.sbm.project.resource.TestProjectContext;
2124
import org.junit.jupiter.api.Test;
2225

2326
import static org.assertj.core.api.Assertions.assertThat;
2427

2528
public class ConvertJaxRsAnnotationsTest {
2629

30+
private final static String SPRING_VERSION = "5.3.13";
31+
32+
@Test
33+
void convertJaxRsMethodWithoutPathToSpringMvc() {
34+
@Language("java")
35+
String restControllerCode = """
36+
package com.example.jeerest.rest;
37+
38+
import com.example.jeerest.Movie;
39+
import com.example.jeerest.MoviesBean;
40+
import org.springframework.beans.factory.annotation.Autowired;
41+
42+
import javax.ws.rs.DELETE;
43+
import javax.ws.rs.GET;
44+
import javax.ws.rs.PUT;
45+
import javax.ws.rs.Path;
46+
import javax.ws.rs.PathParam;
47+
import javax.ws.rs.Produces;
48+
import javax.ws.rs.QueryParam;
49+
import javax.ws.rs.core.MediaType;
50+
import java.util.List;
51+
52+
@Path("movies")
53+
@Produces({"application/json"})
54+
public class MoviesRest {
55+
@GET
56+
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
57+
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
58+
return service.getMovies(first, max, field, searchTerm);
59+
}
60+
}
61+
""";
62+
63+
ProjectContext context = TestProjectContext
64+
.buildProjectContext()
65+
.withJavaSources(restControllerCode)
66+
.withBuildFileHavingDependencies("org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final",
67+
"org.springframework:spring-core:" + SPRING_VERSION)
68+
.build();
69+
70+
Method jaxRsMethod = context.getProjectJavaSources()
71+
.list()
72+
.get(0)
73+
.getTypes()
74+
.get(0)
75+
.getMethods()
76+
.get(0);
77+
78+
ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations
79+
.builder()
80+
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())
81+
.description("Convert JAX-RS annotations into Spring Boot annotations.")
82+
.build();
83+
84+
convertJaxRsAnnotations.convertJaxRsMethodToSpringMvc(jaxRsMethod);
85+
86+
87+
@Language("java")
88+
String expected =
89+
"""
90+
package com.example.jeerest.rest;
91+
92+
import com.example.jeerest.Movie;
93+
import com.example.jeerest.MoviesBean;
94+
import org.springframework.beans.factory.annotation.Autowired;
95+
96+
import javax.ws.rs.DELETE;
97+
import javax.ws.rs.PUT;
98+
import javax.ws.rs.Path;
99+
import javax.ws.rs.PathParam;
100+
import javax.ws.rs.Produces;
101+
import javax.ws.rs.QueryParam;
102+
import javax.ws.rs.core.MediaType;
103+
import java.util.List;
104+
105+
@Path("movies")
106+
@Produces({"application/json"})
107+
public class MoviesRest {
108+
@RequestMapping(method = RequestMethod.GET)
109+
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
110+
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
111+
return service.getMovies(first, max, field, searchTerm);
112+
}
113+
}
114+
""";
115+
116+
assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo(
117+
expected
118+
);
119+
}
120+
27121
@Test
28122
void replaceMethodAnnotationsWithOneAnnotation() throws Exception {
29123

0 commit comments

Comments
 (0)