我正在使用Java Resteasy框架(也使用Jackson)进行REST API。
我试图定义两个几乎相等的API端点:
@POST
@Path("/addbook")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public BookAdvanced addBook (BookAdvanced book){...}
@POST
@Path("/addbook")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public Book addBook (Book book){...}
这可能吗?根据到达的XML内容,我想执行一个或另一个方法。
下面是book类:
package package1;
import javax.xml.bind.annotation.*;
import java.util.Date;
@XmlRootElement(name = "book")
public class Book {
private Long id;
private String name;
private String author;
@XmlAttribute
public void setId(Long id) {
this.id = id;
}
@XmlElement(name = "title")
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "author")
public void setAuthor(String author) {
this.author = author;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
// Constructor, getters and setters
}
下面是BookAdvanced类:
package package1;
import javax.xml.bind.annotation.*;
import java.util.Date;
@XmlRootElement(name = "book")
public class BookAdvanced {
private Long id;
private String name;
private String author;
private int year;
@XmlAttribute
public void setId(Long id) {
this.id = id;
}
@XmlElement(name = "title")
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "author")
public void setAuthor(String author) {
this.author = author;
}
@XmlElement(name = "year")
public void setYear(int year) {
this.year = year;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
// Constructor, getters and setters
}
2023年1月27日12:33:18.238警告[http-nio-8080-exec-39] org.jboss.resteasy.core.registry.SegmentNode.match RESTEASY 002142:多个资源方法匹配请求“POST /hello/addbook”。选择一个。匹配方法:[public package1.BookAdvanced prova_gradle_war.HelloWorldResource.addBook(package1.BookAdvanced),public package1.Book prova_gradle_war.HelloWorldResource.addBook(package1.Book)]
1条答案
按热度按时间xqkwcwgp1#
匹配基于请求URI而不是请求正文。没有真实的的方法来匹配路径并根据正文决定要使用的方法。
您可以手动执行一些操作,检查数据并确定要创建的类型。