2016-06-07 96 views
-3

我是新来的休息API。Rest api如何获取参数?

我需要使一个字符串作为参数,然后返回布尔的api。

现在我的问题是如何将该字符串传递给我的api,然后获取我的api中的字符串?

+1

有很多方法可以将值传递给API。作为URL的一部分,作为查询字符串参数,作为POST值,作为标题值... – David

+0

请发表一些示例代码 –

+0

您介意回答或根据我的回答采取行动吗? –

回答

1

这里有一个例子发生在参数字符串,并有一个默认值,如果不提供查询参数:

@Path("business/department/") 
public interface DepartmentService { 

    @GET 
    @Path("/cs/availability/chat") 
    @Produces(MediaType.APPLICATION_JSON) 
    boolean getCustomerServiceAvailability(@QueryParam("type") @DefaultValue("chat") String type); 
} 

和实现类可以是任何实现的接口。在这个例子中,这是一个无状态的EJB

@Stateless 
public class DepartmentServiceImpl implements DepartmentService { 

@Context 
private HttpServletRequest request; 

private static final Logger LOGGER = Logger.getLogger(DepartmentServiceImpl.class.getName()); 


@Override 
public boolean getCustomerServiceAvailability(String scheduleType) { 

    RequestInfo reqInfo = new RequestInfo(request, this.getClass(), "getCustomerServiceAvailability"); 
    boolean available; 
    try { 
     available = CallBusinessService(scheduleType); 
    } catch (Exception e) { 
     LOGGER.log(Level.SEVERE, e.getLocalizedMessage()); 
     throw new ServiceException(); 
    } finally { 
     reqInfo.logExecutionTime(); 
    } 
} 
}