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...

The serverHost url is the ROP running server' url.now the key point is the parameter list which contains twoparts: one part is the system level parameters:

  • No parmName paramType required desc *1. method String Y API method name(sample.user.add) *2. appKey String Y design to application's appKey,you can define in
    rop.appSecret.properties which is in classpath. *3. v String Y API version,now only support:1.0。 *4. sign String Y API parameters's sing,Use SHA1 encryption algorithm *5. sessionId String N use's sessionId.you can provide a rest api so client can get it and maintain locally. *6. format String N Optional, designated response format. The default XML, currently support for an XML format, json *7. locale String N locale,such lick cn_ZH,en... How to sign the parameter just see:Sign the parameters. the other part is the application level parameters,different from rest api.in the sample,we have three app parameters:
  • userName String
  • password String
  • salary float
Clone this wiki locally