我使用Backbone.js,Bootstrap,NetBeans IDE 8.0,Java EE 7,JDK 8,WildFly服务器8.1.0,JBoss RESTEasy(resteasy-jaxrs-3.0.8),JBoss 2.2.22,JBoss EJB 3制作Web应用程序.
我(相对)是Web开发的新手,因此我才开始掌握很多基本概念和技术.我正在尝试将用户和角色的权限系统构建到Web应用程序中,但我似乎无法使@RolesAllowed注释在我的RESTful Web服务中工作.我已经在这个问题上工作了几天.
我有一个名为UserResource.java的RESTful资源(Java Enterprise/Session Bean?),在这里我有一个方法create来为应用程序创建一个新用户:
import java.net.URI;
import java.security.Principal;
import java.util.List;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.jboss.ejb3.annotation.SecurityDomain;
@Stateless
@SecurityDomain("other")
@Path("/user")
public class UserResource {
@EJB(name = "UserServiceImp")
UserService userService;
@Context
private UriInfo uriInfo;
@RolesAllowed({"admin"})
@Path("create")
@POST
public Response create(CreateRequest request) {
try {
System.out.println("Start of create method");
User user = userService.createUser(request);
return getCreateResponse(user);
}
catch (Exception …Run Code Online (Sandbox Code Playgroud) 我尝试使用CorsFilterResteasy 3.0.9中提供的新功能.我在本页底部找到了一个示例:
使用JAX-RS/RESTEasy实现CORS的Ajax请求
如果我在方法getSingletons()(Application子类)中定义此过滤器,那么我的资源不再被扫描.这意味着将找不到资源并发生以下错误:
javax.ws.rs.NotFoundException: Could not find resource for full path Error Occures
在下一页我找到了一个描述: javax.ws.rs.NotFoundException:找不到完整路径的资源错误Occures
但基本上,此部署选项的作用是扫描应用程序的@ Path,@ Extider等注释.原因是JAX-RS将首先分别在重写的getClasses()和getSingletons()中查找类和对象.如果然后返回空集,则告诉JAX-RS进行扫描(根据规范).
所以如果我覆盖这个getSingletons()方法,JAX-RS不会进行扫描?有没有其他方法来配置它CorsFilter并启用资源扫描?
根据文件,
"客户端是管理客户端通信基础设施的重量级对象.初始化以及客户端实例的处理可能是一项相当昂贵的操作.因此建议在应用程序中仅构建少量客户端实例."
好吧,我正在尝试将Client本身和WebTarget实例缓存在一个静态变量中,someMethod()在多线程环境中调用:
private static Client client = ClientBuilder.newClient();
private static WebTarget webTarget = client.target("someBaseUrl");
...
public static String someMethod(String arg1, String arg2)
{
WebTarget target = entrTarget.queryParam("arg1", arg1).queryParam("arg2", arg2);
Response response = target.request().get();
final String result = response.readEntity(String.class);
response.close();
return result;
}
Run Code Online (Sandbox Code Playgroud)
但有时(并非总是)我得到一个例外:
BasicClientConnManager的使用无效:仍然分配了连接.确保在分配另一个连接之前释放连接.
如何正确地重用/缓存Client/WebTarget?是否可以使用JAX RS Client API?或者我必须使用一些特定于框架的功能(resteasy/jersey)你能提供一些示例或文档吗?
我使用keycloak保证我的休息API,我跟着这个教程以编程方式添加用户,但我得到这个错误信息:
ERROR [io.undertow.request] (default task-9) UT005023: Exception handling request to /service/secured: org.jboss.resteasy.spi.UnhandledException: javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "access_token" (class org.keycloak.representations.AccessTokenResponse), not marked as ignorable (9 known properties: "notBeforePolicy", "otherClaims", "tokenType", "token", "expiresIn", "sessionState", "refreshExpiresIn", "idToken", "refreshToken"])
at [Source: org.apache.http.conn.EofSensorInputStream@9d6aba2; line: 1, column: 18] (through reference chain: org.keycloak.representations.AccessTokenResponse["access_token"])
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.keycloak.adapters.undertow.UndertowAuthenticatedActionsHandler.handleRequest(UndertowAuthenticatedActionsHandler.java:66)
at …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Android应用程序并使用依赖于它的库RESTEasyClient.在运行时使用库时,我得到一个NoSuchMethodError:
java.lang.NoSuchMethodError: No direct method <init>(Ljavax/net/ssl/SSLContext;Lorg/apache/http/conn/ssl/X509HostnameVerifier;)V in class Lorg/apache/http/conn/ssl/SSLSocketFactory; or its super classes (declaration of 'org.apache.http.conn.ssl.SSLSocketFactory' appears in /system/framework/ext.jar)
at org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.initDefaultEngine(ResteasyClientBuilder.java:418)
at org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.build(ResteasyClientBuilder.java:333)
Run Code Online (Sandbox Code Playgroud)
据我了解,这是因为与Android的Apache HttpClient实现不兼容.在线搜索,我发现了各种解决方案,包括org.apache.httpconnections:httpclient在gradle依赖项中,并compile.exclude module: httpclient在几个变体中添加(以解决由此产生的冲突).我已经尝试了所有这些并且总是遇到gradle或NoSuchMethodError上面提到的错误.
是否可以RESTEasyClient在Android 上成功使用?我需要采取哪些步骤才能启用此功能?
我在JBoss AS 6中通过RestEasy使用JAX-RS.当我的JAX-RS资源返回一组项目时(例如通过List),RESTEasy总是使用该名称collection作为根元素.
例如
<collection>
<item>
<description>computer</description>
<price>2500</price>
</item>
<item>
<description>tv</description>
<price>1500</price>
</item>
</collection>
Run Code Online (Sandbox Code Playgroud)
此XML由以下内容生成:
@Produces("application/xml")
@Path("xml")
@RequestScoped
public class MyResource {
@GET
@Path("myitems")
public List<Item> getMyItems() {
return ...
}
}
Run Code Online (Sandbox Code Playgroud)
可以看出,RESTEasy创建的根标记始终是<collection>.
另一方面,泽西岛总是创建一个名称,该名称是列表中包含的元素的复数形式:
<items>
<item>
<description>computer</description>
<price>2500</price>
</item>
<item>
<description>tv</description>
<price>1500</price>
</item>
</items>
Run Code Online (Sandbox Code Playgroud)
我知道可以创建一个包装器类型并返回而不是List,但这是一个相当精细的解决方法,并使代码更复杂.
是否可以轻松指定集合的根标签名称?
RESTEasy(一个JAX-RS实现)有一个很好的客户端框架,例如:
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081");
client.putBasic("hello world");
Run Code Online (Sandbox Code Playgroud)
你如何设置HTTP标头?
澄清:
jkeeler提出的解决方案是一个很好的方法,但我想在ProxyFactory级别设置HTTP标头,我不想将标头传递给客户端对象.有任何想法吗?
我正在尝试编写简单的RESTEasy客户端.下面给出的是示例代码:
Client client = ClientBuilder.newBuilder().build();
WebTarget target = client.target("http://localhost:8080/context/path");
Response response = target.request().post(Entity.entity(object, "application/json"));
//Read output in string format
String value = response.readEntity(String.class);
Run Code Online (Sandbox Code Playgroud)
我在线上得到例外:
Client client = ClientBuilder.newBuilder().build();
Run Code Online (Sandbox Code Playgroud)
我在控制台中看到以下错误:
16:07:57,678 ERROR [stderr] (http-localhost/127.0.0.1:8080-1) java.lang.NoSuchMethodError: org.jboss.resteasy.spi.ResteasyProviderFactory.<init>(Lorg/jboss/resteasy/spi/ResteasyProviderFactory;)V
16:07:57,679 ERROR [stderr] (http-localhost/127.0.0.1:8080-1) at org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration.<init>(ClientConfiguration.java:44)
16:07:57,680 ERROR [stderr] (http-localhost/127.0.0.1:8080-1) at org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.build(ResteasyClientBuilder.java:317)
16:07:57,680 ERROR [stderr] (http-localhost/127.0.0.1:8080-1) at org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.build(ResteasyClientBuilder.java:49)
Run Code Online (Sandbox Code Playgroud)
我在pom.xml中添加了resteasy客户端依赖性:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.8.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的代码部署在JBOSS EAP 6.1上.JDK版本是1.7.
更新:我也尝试在我的pom中添加resteasy jax-rs dependancy.我还通过在web.xml中添加以下行来验证是否启用了ResteasyProviderFactory:
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
请在下面找到maven dependancies列表:
<dependencies>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope> …Run Code Online (Sandbox Code Playgroud) 我想使用嵌入式码头和JAX-RS(无论是resteasy还是jersey)制作RESTful服务.我正在尝试使用maven/eclipse设置创建.如果我尝试关注http://wikis.sun.com/pages/viewpage.action?pageId=21725365链接我无法解决来自ServletHolder sh = new ServletHolder(ServletContainer.class);
public class Main {
@Path("/")
public static class TestResource {
@GET
public String get() {
return "GET";
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
ServletHolder sh = new ServletHolder(ServletContainer.class);
/*
* For 0.8 and later the "com.sun.ws.rest" namespace has been renamed to
* "com.sun.jersey". For 0.7 or early use the commented out code instead
*/
// sh.setInitParameter("com.sun.ws.rest.config.property.resourceConfigClass",
// "com.sun.ws.rest.api.core.PackagesResourceConfig");
// …Run Code Online (Sandbox Code Playgroud) 我正在运行一个Jboss服务器,其中包括一个JAX-RS Web服务(使用与Jboss/EAP捆绑在一起的resteasy-2.3.6.Final).
当客户端中止与服务器的呼叫时,RestEasy的SynchronousDispatcher可以(显然)不发送响应返回给其造成了一堆错误的在客户端[SEVERE]和[ERROR]水平在我的服务器日志显示出来.
但是,我不认为这些是实际错误,所以我不想看到它们.特别是一旦服务将被公开使用,这必然会发生.
这是我想要抑制的日志输出:
12:50:38,938 SEVERE [org.jboss.resteasy.core.SynchronousDispatcher] (http-localhost/127.0.0.1:8080-2) Failed executing GET /ajax/findPerson: org.jboss.resteasy.spi.WriterException: ClientAbortException: java.net.SocketException: Broken pipe
at org.jboss.resteasy.core.ServerResponse.writeTo(ServerResponse.java:262) [resteasy-jaxrs-2.3.6.Final-redhat-1.jar:2.3.6.Final-redhat-1]
....
Caused by: ClientAbortException: java.net.SocketException: Broken pipe
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:403) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:356) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:426) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:415) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
...
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method) [rt.jar:1.7.0_51]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113) [rt.jar:1.7.0_51]
at java.net.SocketOutputStream.write(SocketOutputStream.java:159) [rt.jar:1.7.0_51]
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:711) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:450) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:351) [jbossweb-7.2.0.Final-redhat-1.jar:7.2.0.Final-redhat-1]
...
12:50:38,942 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/myapplication].[MyAjaxApplication]] (http-localhost/127.0.0.1:8080-2) JBWEB000236: Servlet.service() for servlet …Run Code Online (Sandbox Code Playgroud)