参考书籍:《JAVA restful web service实战(第二版)》
第一个rest服务
以jersey-quickstart-grizzly2为原型创建rest服务项目
mvn archetype:generate
-DarchetypeArtifactId=jersey-quickstart-grizzly2
-DarchetypeGroupId=org.glassfish.jersey.archetypes
-DinteractiveMode=false
-DgroupId=my.restful
-DartifactId=my-first-service
-Dpackage=my.restful
-DarchetypeVersion=2.22.1
创建第一个Servlet容器服务
mvn archetype:generate
-DarchetypeArtifactId=jersey-quickstart-webapp
-DarchetypeGroupId=org.glassfish.jersey.archetypes
-DinteractiveMode=false
-DgroupId=my.restful
-DartifactId=my-first-webapp
-Dpackage=my.restful
-DarchetypeVersion=2.22.1
MyResource.java
package my.restful;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.ConcurrentHashMap;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource {
private static ConcurrentHashMap<String, MyDomain> map = new ConcurrentHashMap<>();
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
@GET
@Path("{key}")
@Produces(MediaType.APPLICATION_XML)
public MyDomain getMy(@PathParam("key") final String key) {
final MyDomain myDomain = map.get(key);
if(myDomain == null){
return new MyDomain();
}
return myDomain;
}
@POST
@Consumes(MediaType.APPLICATION_XML)
public void addMy(final MyDomain myDomain) {
map.put(myDomain.getName(), myDomain);
}
}
MyDomain.java
package my.restful;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Created by qifeng009 on 2021/7/13.
*/
@XmlRootElement(name="MyDomain")
@XmlAccessorType(XmlAccessType.FIELD) //必须添加该注解,否则,会报错:
public class MyDomain {
@XmlElement(name="key")
private String name = "aa";
@XmlElement(name="content")
private String value = "bb";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
MyDomain类是基于JAXB的POJO类,用于表示XML格式的表述。
@XmlAccessorType(XmlAccessType.FIELD) 这个注解一定要添加,否则会报错。
报错内容:
严重: Failed to generate the schema for the JAX-B elements
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
类的两个属性具有相同名称 “name”
this problem is related to the following location:
at public java.lang.String my.restful.MyDomain.getName()
at my.restful.MyDomain
this problem is related to the following location:
at private java.lang.String my.restful.MyDomain.name
at my.restful.MyDomain
类的两个属性具有相同名称 “value”
this problem is related to the following location:
at public java.lang.String my.restful.MyDomain.getValue()
at my.restful.MyDomain
this problem is related to the following location:
at private java.lang.String my.restful.MyDomain.value
at my.restful.MyDomain