Restful Web Service部署到weblogic 12c

时间:2022-12-04 21:34:21

介绍一下环境:

首先需要下载一个jaxrs-ri-2.22.2.zip的包

采用Jdeveloper 12c版本,jdk1.8

WebLogic Server 12.2.1版本

 

Restful项目建立

在Jdeveloper 12c中建立一个项目,先建立一个application.

Restful Web Service部署到weblogic 12c

建立一个项目.

Restful Web Service部署到weblogic 12c

在项目中建立一个person的类,在建立一个personService类.

Restful Web Service部署到weblogic 12c

代码如下:

Person.java

package restfulproject;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

private Long id;
private String firstname;
private String lastname;
private Date hiredate;
private Long addressId;

public Person() {
super();
}

public void setId(Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getFirstname() {
return firstname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getLastname() {
return lastname;
}

public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}

public Date getHiredate() {
return hiredate;
}

public void setAddressId(Long addressId) {
this.addressId = addressId;
}

public Long getAddressId() {
return addressId;
}
}

 

PersonService

import java.util.ArrayList;
import java.util.Date;import java.util.List;

public class PersonService {

    private List<Person> persons;
    private Person person;

    public PersonService() {
        super();
        this.persons = new ArrayList<Person>();
        for (long i = 0; i < 10; i++) {
            Person p = new Person();
            p.setId(i);
            p.setFirstname("Firstname " + i);
            p.setLastname("Last " + i);
            p.setHiredate(new Date());
            this.persons.add(p);
        }
        this.person = this.persons.get(0);
    }
 
    public List<Person> getPersons(){
        return this.persons;
    }
 
    public void addPerson(Person person) {
        System.out.println("add person " + person);

        if(person != null){
            getPersons().add(person);
        }
    }    public Person getPerson(){
        return person;
    }
}

生成restful Service.

Restful Web Service部署到weblogic 12c

 

Restful Web Service部署到weblogic 12c

选择consume和product支持的格式

Restful Web Service部署到weblogic 12c

 

修改代码最后变成:

package restfulproject;

import java.util.ArrayList;
import java.util.Date;import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("restfulproject")
@Consumes(value = { "application/json", "application/xml" })
@Produces(value = { "application/json", "application/xml" })
public class PersonService {

private List<Person> persons;
private Person person;

public PersonService() {
super();
this.persons = new ArrayList<Person>();
for (long i = 0; i < 10; i++) {
Person p = new Person();
p.setId(i);
p.setFirstname("Firstname " + i);
p.setLastname("Last " + i);
p.setHiredate(new Date());
this.persons.add(p);
}
this.person = this.persons.get(0);
}

@GET
@Path("/Persons")
public List<Person> getPersons(){
return this.persons;
}

@POST
@Path("/person")

@Consumes(MediaType.APPLICATION_JSON)

public void addPerson(Person person) {

if(person != null){
System.out.println("Enter====");
System.out.println(persons.size());
System.out.println(person.getFirstname());

this.persons.add(person);
System.out.println(persons.size());
System.out.println("Over====");

}

 

}

@GET
@Path("/person/{userid}")
public Person getPerson(@PathParam("userid") int userid){
List<Person> personlist = this.getPersons();
for(Person user: personlist){
if(user.getId() == userid){
return user;
}
}
return null;
}
}

 

同时web.xml也加入了内容

<?xml version = '1.0' encoding = 'GBK'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

 

部署

 

生成部署profile.

Restful Web Service部署到weblogic 12c

选择生成War包

Restful Web Service部署到weblogic 12c

 

然后部署.

Restful Web Service部署到weblogic 12c

 

测试

在WebLogic管理控制台上找到部署的url

Restful Web Service部署到weblogic 12c

然后找到WADL地址

Restful Web Service部署到weblogic 12c

 

通过SOAPUI生成WADL项目,然后运行测试Perons,获取所有的person信息

Restful Web Service部署到weblogic 12c

 

如果需要得到xml,需要在head中加入Accept=application/xml

Restful Web Service部署到weblogic 12c

 

getperson/{userid}的测试界面

Restful Web Service部署到weblogic 12c

 

添加用户

Restful Web Service部署到weblogic 12c