Rest学习一

参考书籍:《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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值