Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaWeb项目中创建web服务,并使用它为android应用程序提供服务_Java_Web Services - Fatal编程技术网

如何在JavaWeb项目中创建web服务,并使用它为android应用程序提供服务

如何在JavaWeb项目中创建web服务,并使用它为android应用程序提供服务,java,web-services,Java,Web Services,实际上,我正在尝试为android应用程序提供web服务。我试过做下面给出的事情 这是我的Person.java实体 package com.avilyne.rest.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Person { public String getFirstName() { return firstName;

实际上,我正在尝试为android应用程序提供web服务。我试过做下面给出的事情

这是我的Person.java实体

    package com.avilyne.rest.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    public Person() {

        id = -1;
        firstName = "";
        lastName = "";
        email = "";

    }

    public Person(long id, String firstName, String lastName, String email) {

        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }


    private long id;
    private String firstName;
    private String lastName;
    private String email;

}
这是我的人力资源

package com.avilyne.rest.resource;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Request;

import com.avilyne.rest.model.Person;

@Path("/person")
public class PersonResource {

    private final static String FIRST_NAME = "firstName";
    private final static String LAST_NAME = "lastName";
    private final static String EMAIL = "email";

    private Person person = new Person(1, "Sample", "Person", "sample_person@jerseyrest.com");

    // The @Context annotation allows us to have certain contextual objects
    // injected into this class.
    // UriInfo object allows us to get URI information (no kidding).
    @Context
    UriInfo uriInfo;

    // Another "injected" object. This allows us to use the information that's
    // part of any incoming request.
    // We could, for example, get header information, or the requestor's address.
    @Context
    Request request;

    // Basic "is the service running" test
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String respondAsReady() {
        return "Demo service is ready!";
    }

    @GET
    @Path("sample")
    @Produces(MediaType.APPLICATION_JSON)
    public Person getSamplePerson() {

        System.out.println("Returning sample person: " + person.getFirstName() + " " + person.getLastName());

        return person;
    }

    // Use data from the client source to create a new Person object, returned in JSON format.  
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Person postPerson(
            MultivaluedMap<String, String> personParams
            ) {

        String firstName = personParams.getFirst(FIRST_NAME);
        String lastName = personParams.getFirst(LAST_NAME);
        String email = personParams.getFirst(EMAIL);

        System.out.println("Storing posted " + firstName + " " + lastName + "  " + email);

        person.setFirstName(firstName);
        person.setLastName(lastName);
        person.setEmail(email);

        System.out.println("person info: " + person.getFirstName() + " " + person.getLastName() + " " + person.getEmail());

        return person;

    }
}
package com.avilyne.rest.resource;
导入javax.ws.rs.Path;
导入javax.ws.rs.products;
导入javax.ws.rs.Consumes;
导入javax.ws.rs.GET;
导入javax.ws.rs.POST;
导入javax.ws.rs.PathParam;
导入javax.ws.rs.core.Context;
导入javax.ws.rs.core.MediaType;
导入javax.ws.rs.core.MultivaluedMap;
导入javax.ws.rs.core.UriInfo;
导入javax.ws.rs.core.Request;
导入com.avilyne.rest.model.Person;
@路径(“/人”)
公共类人员资源{
私有最终静态字符串FIRST_NAME=“firstName”;
私有最终静态字符串LAST\u NAME=“lastName”;
私有最终静态字符串EMAIL=“EMAIL”;
私人人员=新人(1,“样本”,“人员”,“样本_person@jerseyrest.com");
//@Context注释允许我们拥有某些上下文对象
//注入到这个类中。
//UriInfo对象允许我们获取URI信息(不是开玩笑的)。
@上下文
UriInfo-UriInfo;
//另一个“注入”对象。这允许我们使用
//任何传入请求的一部分。
//例如,我们可以获取标题信息或请求者的地址。
@上下文
请求;
//基本的“服务是否正在运行”测试
@得到
@生成(MediaType.TEXT\u PLAIN)
公共字符串respondAsReady(){
return“演示服务准备就绪!”;
}
@得到
@路径(“样本”)
@产生(MediaType.APPLICATION_JSON)
公众人物getSamplePerson(){
System.out.println(“返回示例person:+person.getFirstName()+”+person.getLastName());
返回人;
}
//使用来自客户端源的数据创建一个新的Person对象,该对象以JSON格式返回。
@职位
@使用(MediaType.APPLICATION\u FORM\u URLENCODED)
@产生(MediaType.APPLICATION_JSON)
公众人士邮递员(
多值映射personParams
) {
String firstName=personParams.getFirst(FIRST_NAME);
字符串lastName=personParams.getFirst(姓氏);
字符串email=personParams.getFirst(email);
System.out.println(“存储已发布”+firstName+“”+lastName+“”+电子邮件);
person.setFirstName(firstName);
person.setLastName(lastName);
person.setEmail(email);
System.out.println(“人员信息:”+person.getFirstName()+“”+person.getLastName()+“”+person.getEmail());
返回人;
}
}
这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID"
    version="3.0">
  <display-name>JerseyRESTServer</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.avilyne.rest.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

JerseyRESTServer
泽西岛休息服务
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
com.avilyne.rest.resource
1.
泽西岛休息服务
/休息/*
运行此代码后,我得到404(链接未找到错误)。 但当我使用吼叫链接运行此代码时,它会在浏览器上显示index.jsp的内容

但我想访问控制器,而我在访问控制器的链接中并没有得到要修改的内容。 图片显示了我的项目的文件结构


我想访问PersonResource.java的方法,是在
/JerseyRESTServer
下安装的webapp吗?看起来可能是URL需要
http://localhost:8080/rest/person
。我也试过了。但它还是给了我404error@kenney正在给我inde.php的内容。我现在已经更新了我的代码请帮助我您可能想尝试将所有JAR添加到WEB-INF/libI中。我尝试过这样做。但是文件没有粘贴到lib中。是在
/JerseyRESTServer
下安装的webapp吗?看起来可能是URL需要
http://localhost:8080/rest/person
。我也试过了。但它还是给了我404error@kenney正在给我inde.php的内容。我现在已经更新了我的代码请帮助我您可能想尝试将所有JAR添加到WEB-INF/libI中。我尝试过这样做。但文件并没有粘贴到lib中。