Skip to content

Five minutes quick start

itstamen edited this page Mar 2, 2012 · 39 revisions

#case descripton We need to create new account through Rop framework. #Create a rest api:

Step 1:create the rop request of create new user:

    package com.stamen.sample.rop.request;

import com.stamen.rop.RopRequest;
import org.springframework.format.annotation.NumberFormat;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Pattern;

public class CreateUserRequest extends RopRequest {

@Pattern(regexp = "\\w{4,30}")//① constraint of the field
private String userName;

@Pattern(regexp = "\\w{6,30}")//②
private String password;
 
@DecimalMin("1000.00")
@DecimalMax("100000.00")
@NumberFormat(pattern = "#,###.##")//③
private long salary;

//getter and setter...
}

The ①、② and ③ is the JSR 303 annotation,ROP use the JSR 303 to validate the request data.If the request datais invalid ,ROP will generate the standard error respone(see the ROP error system for detail).

Step 2:create rop response of create new user

package com.stamen.sample.rop;

import com.stamen.rop.RopResponse;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD) //①
@XmlRootElement(name = "sampleRopResponse1")
public class CreateUserResponse implements RopResponse{
	@XmlAttribute
	private String userId;
	@XmlAttribute
	private String createTime;
   //getter and setter...
}

The ① is the JAXB annotation,ROP use the JAXB to marshaller respone to corresponding format respone message.currently ROP supoort xml and json format message.

Step 3:create the rop rest service:

package com.stamen.sample.rop;

import com.stamen.rop.ApiMethod;
import com.stamen.sample.rop.request.CreateUserRequest;
import com.stamen.sample.rop.response.CreateUserResponse;
import org.springframework.stereotype.Service;

@Service//① Let it be a Spring Bean
public class UserRestService {

	@ApiMethod("sample.user.add")//② Let this method service the 

sample.user.add method
	public RopResponse addUser(CreateUserRequest request) {
		CreateUserResponse response = new CreateUserResponse();
		//add creaet new user here...
		response.setCreateTime("20120101010101");
		response.setUserId("1");
		return response;
	}
}

The last thing we need to to is create Rest service to handle the rest request and return respone.

First in the ① ,we put the @Service to make the UserRestService be a Spring Bean,so ROP can scan for candidate api handler.Secondly,create a rest api handler method which is addUser,the method 's input parameter must extends from the RopRequest.The method's return Class must be RopResponse.

So far web have finished the rest api.We start the web container,the rest api is repare for service.

write client code

client just send the corresponding HTTP request using like following: http://serverHost?param1=value1&param2=value2...

Clone this wiki locally