使用EJB和JAX-RS在java中使用企业应用程序时最好的Maven原型是什么,考虑到在不久的将来我将不得不使用JAX-RS作为后端EJB使用JSF2中的前端?
我一直在互联网上寻找一个如何做到这一点的例子.我只想设置一个REST服务器,它自动将对象与XML进行序列化.我只是尝试提供一个服务器,只有在用户登录后才能使用户登录,注销和访问XML对象列表.获得一个简单的示例应用程序需要什么?
我没有理解Restlet库的工作方式,而且我完全不习惯使用JAXB和JAXRS.我参与了一个使用这些库的项目,但仅从客户的角度来看.
我正在使用JAX-RS的平针织实现来进行Web服务.我是这个JAX-RS的新手.
我正在尝试在服务中添加一个接受Employee对象的方法,并根据Employee对象值返回雇员Id(此时有一个DB命中).
遵循Restful原则,我将方法设为@GET并提供了url路径,如下所示:
@Path("/EmployeeDetails")
public class EmployeeService {
@GET
@Path("/emp/{param}")
public Response getEmpDetails(@PathParam("param") Employee empDetails) {
//Get the employee details, get the db values and return the Employee Id.
return Response.status(200).entity("returnEmployeeId").build();
}
}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我写了这个客户端:
public class ServiceClient {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
Employee emp = new Employee();
emp.name = "Junk Name";
emp.age = "20";
System.out.println(service.path("rest").path("emp/" + emp).accept(MediaType.TEXT_PLAIN).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8045/AppName").build(); …Run Code Online (Sandbox Code Playgroud) 我在休息服务中看到了以下代码片段
@POST
@Consumes("application/xml")
@Path("/ProjectConfiguration/{onetimetoken}")
public void createProjectConfiguration(@PathParam("onetimetoken") String oneTimeToken,
CreateProjectConfigurationRequest request) throws IntegratedAppFault {
// NOOPS
}
Run Code Online (Sandbox Code Playgroud)
在这个变量值中,oneTimeToken由路径中的内容初始化.但我怀疑的是如何初始化CreateProjectConfigurationRequest类型的请求对象.我们如何自由地在这些方法中拥有任何参数.这是怎么回事?如何保证初始化.
我正在使用jboss 7.1和resteasy进行基于令牌的身份验证.我正在使用PreProcessInterceptor来拦截请求,获取令牌,从令牌中检索用户,然后根据放置在方法上的自定义注释检查用户角色.我现在要做的是将User注入方法,如下所示.
@Path("/doStuffWithUser")
@GET
@Requires("ADMIN") // custom annotation
public Response doStuffWithUser(@Context User user);
Run Code Online (Sandbox Code Playgroud)
我知道这与这个问题非常接近,我尝试在链接的github示例中添加了不同的解决方案,但是我找不到从PreProcessInterceptor中注入用户的方法.
谢谢
我正在使用带有Dropwizard 0.8.0-rc1的JAX-RS 2.0,我真的无法弄清楚如何从javax.ws.rs.core.Response对象中拉出我的实体.response.getEntity()给了我ByteArrayOutputStream.我可以创建两个请求 - 一个给我标题和链接,另一个给我我的响应实体,但它似乎是一个愚蠢,浪费和不清楚的事情.有没有办法从响应对象中获取实体?
我目前的测试代码如下:
public class GroupsResourceTest {
public static String CONFIGURATION_FILE = "src/test/resources/test-conf.yml";
@ClassRule
public final static DropwizardAppRule<BpmConsoleConfiguration> RULE =
new DropwizardAppRule<>(BpmConsoleApplication.class, CONFIGURATION_FILE);
static Client client;
@BeforeClass
public static void initClient(){
client = new JerseyClientBuilder(RULE.getEnvironment()).build("client");
client.register(HttpAuthenticationFeature.basic(User.ADMIN.login, User.ADMIN.password));
}
@Test
public void shouldGetGroups() {
//when
WebTarget resource = target("/groups");
List<String> groups = resource.request().get(new GenericType<>(List.class)); //first request
Response response = resource.request().get(); //second request
Link self = response.getLink("self");
//then
assertThat(self.getUri().getPath()).isEqualTo("/groups");
assertThat(groups).contains(User.ADMIN.login);
}
public WebTarget …Run Code Online (Sandbox Code Playgroud) 我有一个需要验证的网址(例如出现浏览器弹出窗口,询问用户名和密码。)

通常,我们可以使用以下格式来实现此目的:
http://username:password@url.com
使用RESTEasy客户端构建器
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://url.com");
Run Code Online (Sandbox Code Playgroud)
如何在不构建http://username:password@url.com自己的情况下实现它 ?我看过是否有任何方法可以用来设置它,但是没有运气。另外,我不确定如何传递这些凭据,标题,Cookie等。
我在请求中有以下HTTP标头,我想从中提取JSESSIONID它:
Accept=application/json
Accept-Encoding=gzip
deflate,Accept-Language=en-us
Connection=keep-alive
Content-Length=0
Content-Type=application/json
Cookie=JSESSIONID=ss0ox8w99o9142b73rssvc0r
Host=localhost:8080
User-Agent=NFA_QA/34 CFNetwork/720.5.7 Darwin/14.5.0 (x86_64)
Run Code Online (Sandbox Code Playgroud)
我使用ContainerRequestContext如下:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
System.out.println("***HEADER VALUE**: " + requestContext.getHeaderString("Cookie"));
}
Run Code Online (Sandbox Code Playgroud)
获得结果:
JSESSIONID=zv71od6l2fd41hv6yf0980khy
Run Code Online (Sandbox Code Playgroud)
和:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
Map<String, javax.ws.rs.core.Cookie> clientCookie = requestContext.getCookies();
System.out.println("Client Cookie Map: " + clientCookie);
}
Run Code Online (Sandbox Code Playgroud)
获得结果:
Clinet Cookie Map: {JSESSIONID=JSESSIONID=1of1x5u1s1l4hdxfg2azlep42}
Run Code Online (Sandbox Code Playgroud)
JSESSIONID从请求中提取的最佳方法是什么?
我试图在Eclipse中使用JAX-RS从Java REST API方法返回多个对象(如String,Boolean,MyOwnClass等).
这就是我现在所拥有的:
我的API方法
@Path("/")
public class myAPI {
@GET
@Produces({ "application/xml", "application/json" })
@Path("/getusers")
public Response GetAllUsers() {
//Data Type #1 I need to send back to the clients
RestBean result = GetAllUsers();
//Data Type #2 I need to send with in the response
Boolean isRegistered = true;
//The following code line doesn't work. Probably wrong way of doing it
return Response.ok().entity(result, isRegistered).build();
}
}
Run Code Online (Sandbox Code Playgroud)
RestBean类:
public class RestBean {
String status = "";
String description = ""; …Run Code Online (Sandbox Code Playgroud) 我有一个资源:
@GET
@Path("/print-order")
@Produces("application/pdf")
public byte[] printOrder(@QueryParam("id") Long orderId) {
return ...;
}
Run Code Online (Sandbox Code Playgroud)
...这可能会引发与用户相关的错误,并且必须显示为HTML页面。所以我实现了一个,ExceptionMapper但我不知道如何获取@Produces("application/pdf")被调用资源的注释的值。
@Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {
@Override
public Response toResponse(CustomException exception) {
if (contentType = "application/pdf")
... html respone
else
... entity response
}
}
Run Code Online (Sandbox Code Playgroud)
我在Jersy 1.12实现中使用JAX-RS 1.x(jsr311),但希望拥有独立于实现的解决方案。