Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ recipeDependencies {
parserClasspath("io.micrometer:micrometer-observation:1.11.+")
parserClasspath("io.springfox:springfox-swagger2:3.+")
parserClasspath("io.swagger.core.v3:swagger-models:2.+")
parserClasspath("javax.ws.rs:javax.ws.rs-api:2.1.1")
parserClasspath("jakarta.ws.rs:jakarta.ws.rs-api:4.0.0")

testParserClasspath("com.fasterxml.jackson.core:jackson-core:2.20.+")
testParserClasspath("com.nimbusds:nimbus-jose-jwt:9.13")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.mvc;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.tree.*;

import java.util.*;

@Value
@EqualsAndHashCode(callSuper = false)
public class JaxrsToSpringmvcAnnotations extends Recipe {

String displayName = "Migrate jax-rs annotations to spring MVC annotations";
String description = "Replaces all jax-rs annotations with Spring MVC annotations.";
Set<String> tags = new HashSet<>(Arrays.asList("Java", "Spring"));

private static final List<AnnotationMatcher> PATH_ANNOTATION_MATCHERS = Arrays.asList(
new AnnotationMatcher("@jakarta.ws.rs.Path"),
new AnnotationMatcher("@javax.ws.rs.Path"));

private static final Map<String, String> ANNOTATION_MAPPING = new HashMap<>();

static {
ANNOTATION_MAPPING.put("jakarta.ws.rs.GET", "GetMapping");
ANNOTATION_MAPPING.put("jakarta.ws.rs.POST", "PostMapping");
ANNOTATION_MAPPING.put("jakarta.ws.rs.PUT", "PutMapping");
ANNOTATION_MAPPING.put("jakarta.ws.rs.DELETE", "DeleteMapping");
ANNOTATION_MAPPING.put("javax.ws.rs.GET", "GetMapping");
ANNOTATION_MAPPING.put("javax.ws.rs.POST", "PostMapping");
ANNOTATION_MAPPING.put("javax.ws.rs.PUT", "PutMapping");
ANNOTATION_MAPPING.put("javax.ws.rs.DELETE", "DeleteMapping");
}

private static final Map<String, String> PARAM_ANNOTATION_MAPPING = new HashMap<>();

static {
PARAM_ANNOTATION_MAPPING.put("jakarta.ws.rs.QueryParam", "RequestParam");
PARAM_ANNOTATION_MAPPING.put("jakarta.ws.rs.FormParam", "RequestParam");
PARAM_ANNOTATION_MAPPING.put("jakarta.ws.rs.PathParam", "PathVariable");
PARAM_ANNOTATION_MAPPING.put("jakarta.ws.rs.HeaderParam", "RequestHeader");
PARAM_ANNOTATION_MAPPING.put("javax.ws.rs.QueryParam", "RequestParam");
PARAM_ANNOTATION_MAPPING.put("javax.ws.rs.FormParam", "RequestParam");
PARAM_ANNOTATION_MAPPING.put("javax.ws.rs.PathParam", "PathVariable");
PARAM_ANNOTATION_MAPPING.put("javax.ws.rs.HeaderParam", "RequestHeader");
}

private static final String ANNOTATION_PREFIX = "org.springframework.web.bind.annotation.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);

List<String> annotationArgs = new ArrayList<>();
List<Expression> args = new ArrayList<>();
getArgumentsForRequestMappings(cd.getLeadingAnnotations(), annotationArgs, args);
if (annotationArgs.isEmpty()) {
return cd;
}
String annTemplate = "@RestController\n@RequestMapping(" + String.join(", ", annotationArgs) + ")";

JavaCoordinates coordinates = cd.getCoordinates().addAnnotation((o1, o2) -> 1);
maybeAddImport("org.springframework.web.bind.annotation.RequestMapping");
maybeAddImport("org.springframework.web.bind.annotation.RestController");
return JavaTemplate.builder(annTemplate)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-web"))
.imports("org.springframework.web.bind.annotation.RestController",
"org.springframework.web.bind.annotation.RequestMapping")
.build()
.apply(updateCursor(cd), coordinates, args.toArray());
}

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration md, ExecutionContext ctx) {
String jaxrsAnnotation = null;
for (String jaxrs : ANNOTATION_MAPPING.keySet()) {
Optional<J.Annotation> annOpt = md.getLeadingAnnotations().stream()
.filter(a -> TypeUtils.isOfClassType(a.getType(), jaxrs))
.findFirst();
if (annOpt.isPresent()) {
jaxrsAnnotation = jaxrs;
break;
}
}
if (jaxrsAnnotation == null) {
return md;
}
String springAnnotation = ANNOTATION_MAPPING.get(jaxrsAnnotation);
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + jaxrsAnnotation)));
maybeRemoveImport(jaxrsAnnotation);

List<String> annotationArgs = new ArrayList<>();
List<Expression> args = new ArrayList<>();
Expression consumes = getArgumentsForRequestMappings(md.getLeadingAnnotations(), annotationArgs, args);
String annTemplate;
if (annotationArgs.isEmpty()) {
annTemplate = "@" + springAnnotation;
} else {
annTemplate = "@" + springAnnotation + "(" + String.join(", ", annotationArgs) + ")";
}


JavaCoordinates coordinates = md.getCoordinates().addAnnotation((o1, o2) -> 1);
md = JavaTemplate.builder(annTemplate)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-web"))
.imports(ANNOTATION_PREFIX + springAnnotation)
.build()
.apply(updateCursor(md), coordinates, args.toArray());
maybeAddImport(ANNOTATION_PREFIX + springAnnotation);

getCursor().putMessage("methodParams", md.getParameters());
getCursor().putMessage("isPutOrPost", jaxrsAnnotation.endsWith("PUT") || jaxrsAnnotation.endsWith("POST"));
getCursor().putMessage("needRequestPart", isMultiPart(consumes));
return super.visitMethodDeclaration(md, ctx);
}

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDecl, ExecutionContext ctx) {
J.VariableDeclarations vd = super.visitVariableDeclarations(variableDecl, ctx);

List<Statement> methodParams = getCursor().getNearestMessage("methodParams");
if (methodParams == null || !methodParams.contains(vd)) {
return vd;
}

String springAnnotation = null;
Expression value = null;
Expression defaultValue = null;
boolean isPutOrPost = Boolean.TRUE.equals(getCursor().getNearestMessage("isPutOrPost"));
boolean needRequestPart = Boolean.TRUE.equals(getCursor().getNearestMessage("needRequestPart"));
boolean isContextParam = false;
for (J.Annotation ann : vd.getLeadingAnnotations()) {
String annType = ann.getType() != null ? ann.getType().toString() : "";
if (PARAM_ANNOTATION_MAPPING.containsKey(annType)) {
springAnnotation = PARAM_ANNOTATION_MAPPING.get(annType);
value = getValueFromAnnotation(ann);
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + annType)));
maybeRemoveImport(annType);
} else if ("jakarta.ws.rs.DefaultValue".equals(annType) || "javax.ws.rs.DefaultValue".equals(annType)) {
defaultValue = getValueFromAnnotation(ann);
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + annType)));
maybeRemoveImport(annType);
} else if ("jakarta.ws.rs.core.Context".equals(annType) || "javax.ws.rs.core.Context".equals(annType)) {
isContextParam = true;
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + annType)));
maybeRemoveImport(annType);
}
}

StringBuilder annBuilder = null;
List<Expression> args = new ArrayList<>();
if (springAnnotation != null) {
args.add(value);
annBuilder = new StringBuilder("@" + springAnnotation + "(");
if ("PathVariable".equals(springAnnotation)) {
annBuilder.append("#{any(String)})");
} else {
if (defaultValue == null) {
annBuilder.append("value = #{any(String)}, required = false)");
} else {
annBuilder.append("value = #{any(String)}, defaultValue = #{any()})");
args.add(defaultValue);
}
}
} else if (!isContextParam && isPutOrPost) {
if (needRequestPart) {
springAnnotation = "RequestPart";
} else {
springAnnotation = "RequestBody";
}
annBuilder = new StringBuilder("@" + springAnnotation);
}
if (annBuilder != null) {
JavaCoordinates coordinates = vd.getCoordinates().addAnnotation((o1, o2) -> 1);
vd = JavaTemplate.builder(annBuilder.toString())
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-web"))
.imports(ANNOTATION_PREFIX + springAnnotation)
.build()
.apply(updateCursor(vd), coordinates, args.toArray());
maybeAddImport(ANNOTATION_PREFIX + springAnnotation);
}

return vd;
}

private @Nullable Expression getArgumentsForRequestMappings(List<J.Annotation> annotations, List<String> annotationArgs, List<Expression> args) {
Expression path = null;
Expression produces = null;
Expression consumes = null;

for (J.Annotation ann : annotations) {
String annType = ann.getType() != null ? ann.getType().toString() : "";
switch (annType) {
case "jakarta.ws.rs.Path":
case "javax.ws.rs.Path":
path = getValueFromAnnotation(ann);
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + annType)));
maybeRemoveImport(annType);
break;
case "jakarta.ws.rs.Consumes":
case "javax.ws.rs.Consumes":
consumes = getValueFromAnnotation(ann);
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + annType)));
maybeRemoveImport(annType);
break;
case "jakarta.ws.rs.Produces":
case "javax.ws.rs.Produces":
produces = getValueFromAnnotation(ann);
doAfterVisit(new RemoveAnnotationVisitor(new AnnotationMatcher("@" + annType)));
maybeRemoveImport(annType);
break;
}
}

if (path != null && produces == null && consumes == null) {
annotationArgs.add("#{any(String)}");
args.add(path);
} else {
if (path != null) {
annotationArgs.add("value = #{any(String)}");
args.add(path);
}
if (produces != null) {
annotationArgs.add("produces = #{any()}");
args.add(produces);
}
if (consumes != null) {
annotationArgs.add("consumes = #{any()}");
args.add(consumes);
}
}

return consumes;
}

private @Nullable Expression getValueFromAnnotation(J.Annotation ann) {
if (ann.getArguments() == null || ann.getArguments().isEmpty()) {
return null;
}
Expression arg = ann.getArguments().get(0);
if (arg instanceof J.Assignment) {
return ((J.Assignment) arg).getAssignment();
}
return arg;
}

private boolean isMultiPart(@Nullable Expression consumes) {
if (consumes == null) {
return false;
}
if (consumes instanceof J.NewArray) {
J.NewArray arr = (J.NewArray) consumes;
if (arr.getInitializer() != null && arr.getInitializer().size() == 1) {
consumes = arr.getInitializer().get(0);
} else {
return false;
}
}
return consumes.toString().toLowerCase().contains("multipart");
}

};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.spring.mvc;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.ChangeType;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaCoordinates;

import java.util.Arrays;
import java.util.List;

@Value
@EqualsAndHashCode(callSuper = false)
public class JaxrsToSpringmvcMediaType extends Recipe {

String displayName = "Migrate jax-rs MediaType to spring MVC MediaType";
String description = "Replaces all jax-rs MediaType with Spring MVC MediaType.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("*.ws.rs.core.MediaType", true), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.FieldAccess visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
String typeName = fieldAccess.getTarget().getType() != null ? fieldAccess.getTarget().getType().toString() : "";
if ("javax.ws.rs.core.MediaType".equals(typeName) || "jakarta.ws.rs.core.MediaType".equals(typeName)) {
maybeRemoveImport(typeName);
maybeAddImport("org.springframework.http.MediaType");

String field = fieldAccess.getName().getSimpleName();
String newField = field.endsWith("_TYPE") ? field.substring(0, field.length() - "_TYPE".length()) : field + "_VALUE";
JavaCoordinates coordinates = fieldAccess.getCoordinates().replace();
return JavaTemplate.builder("MediaType.#{}")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "spring-web"))
.imports("org.springframework.http.MediaType")
.build()
.apply(getCursor(), coordinates, newField);
}
return super.visitFieldAccess(fieldAccess, ctx);
}
});
}

@Override
public List<Recipe> getRecipeList() {
return Arrays.asList(
new ChangeType("javax.ws.rs.core.MediaType", "org.springframework.http.MediaType", true),
new ChangeType("jakarta.ws.rs.core.MediaType", "org.springframework.http.MediaType", true)
);
}
}
Loading
Loading