27
27
/**
28
28
* @author Fabian Krüger
29
29
*/
30
- public class ConvertJaxRsAnnotationsest {
30
+ public class ConvertJaxRsAnnotationsTest {
31
31
32
32
private final static String SPRING_VERSION = "5.3.13" ;
33
33
@@ -73,7 +73,8 @@ public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("ma
73
73
.withJavaSources (restControllerCode )
74
74
.withBuildFileHavingDependencies (
75
75
"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
77
78
)
78
79
.build ();
79
80
@@ -94,20 +95,20 @@ public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("ma
94
95
import com.example.jeerest.Movie;
95
96
import com.example.jeerest.MoviesBean;
96
97
import org.springframework.beans.factory.annotation.Autowired;
98
+ import org.springframework.web.bind.annotation.RequestMapping;
99
+ import org.springframework.web.bind.annotation.RequestMethod;
97
100
import org.springframework.web.bind.annotation.RestController;
98
101
99
102
import javax.ws.rs.DELETE;
100
103
import javax.ws.rs.PUT;
101
104
import javax.ws.rs.PathParam;
102
- import javax.ws.rs.Produces;
103
105
import javax.ws.rs.QueryParam;
104
106
import javax.ws.rs.core.MediaType;
105
107
import java.util.List;
106
108
107
109
108
- @Produces({"application/json"})
109
110
@RestController
110
- @RequestMapping(value = "movies")
111
+ @RequestMapping(value = "movies", produces = {"application/json"} )
111
112
public class MoviesRest {
112
113
113
114
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@@ -119,15 +120,120 @@ public Movie find(@PathParam("id") Long id) {
119
120
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
120
121
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
121
122
return service.getMovies(first, max, field, searchTerm);
122
- }
123
+ }
123
124
}
124
125
""" ;
125
126
126
127
assertThat (context .getProjectJavaSources ().list ().get (0 ).print ()).isEqualTo (
127
128
expected
128
129
);
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
+ """ ;
129
148
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 ();
130
157
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
+ );
131
185
}
132
186
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
+ }
133
239
}
0 commit comments