【问题标题】:Rest api how to get parameters?Rest api如何获取参数?
【发布时间】:2016-06-07 12:09:53
【问题描述】:

我是rest api的新手。

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

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

【问题讨论】:

  • 有很多方法可以将值传递给 API。作为 URL 的一部分,作为查询字符串参数,作为 POST 值,作为标头值...
  • 请发布一些示例代码
  • 您介意回答我的回答还是采取行动?

标签: rest parameters restful-url getparameter


【解决方案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();
    }
}
}

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2015-05-13
    • 2019-12-01
    • 1970-01-01
    • 2015-10-31
    • 2015-10-22
    相关资源
    最近更新 更多