我在@Path中使用了正则表达式来实现重载,起初我认为它非常简洁,但是重载方法通常不是很好的做法.这同样适用于RESTful Web服务吗?有没有更好的方法来实现这个使用JAX-RS?
所以我现在可以通过/ project/ProjectNumber1000或/ project/12345调用我的getProject REST服务
@Path("/project")
public class ProjectPropertiesResource
{
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{name : [a-zA-Z]+}")
public Response getProjectPropertiesByName(@PathParam("name") String name)
{
...
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{id : \\d+}")
public Response getProjectPropertiesById(@PathParam("id") long id)
{
...
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个使用jar-rs的web服务.如何将自定义http错误代码抛出到调用应用程序?
谢谢
我补充道
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs-all</artifactId>
<version>2.2.1.GA</version>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我正在使用
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
Run Code Online (Sandbox Code Playgroud)
当我尝试构建时,我收到以下错误.我究竟做错了什么?
[错误]无法在项目tapvox-api上执行目标:无法解析项目com.myproject.api的依赖关系:myproject-api:war:1.0-SNAPSHOT:找不到工件org.jboss.resteasy:resteasy-jaxrs-all :jar:2.2.1.GA in jboss(http://repository.jboss.org/nexus/content/groups/public) - > [帮助1]
我当前的代码是
@Path("login")
@RequestScoped
public class LoginResource {
@GET
@SecurityChecked
public Response getUser(@HeaderParam("AUTH") @Nonnull final String authToken) {
return Response.ok("authenticated successfully.").build();
}
}
Run Code Online (Sandbox Code Playgroud)
并且@SecurityChecked是自定义注释
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@Inherited
@InterceptorBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface SecurityChecked {
}
Run Code Online (Sandbox Code Playgroud)
和拦截器类为
@Interceptor
@SecurityChecked
public class SecurityCheckInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger("SecurityCheckInterceptor");
@AroundInvoke
public Object validateUser(final InvocationContext context) throws Exception {
final Object[] params = context.getParameters();
LOGGER.info("Authentication token: " …Run Code Online (Sandbox Code Playgroud) 如果客户端连接到这样的Web服务(JAX-RS):
URL u = new URL(server);
URLConnection con = u.openConnection();
con.setDoOutput(true);
con.getOutputStream().write(stream.toByteArray()); // ByteArrayOutputStream
con.connect();
InputStream inputStream = con.getInputStream();
byte [] urlBytes = new byte [inputStream.available()];
inputStream.read(urlBytes);
url = new String(urlBytes);
Run Code Online (Sandbox Code Playgroud)
那么Web服务接口定义应该是什么?我有:
@POST
@Path("/upload")
@Consumes("image/jpeg")
@Produces(MediaType.TEXT_PLAIN)
String uploadPicture();
Run Code Online (Sandbox Code Playgroud)
但是,当客户端访问时,它会抛出:
[错误]信息:生成jax-rs代理加载器类.[INFO] DEBUG [SynchronousDispatcher] PathInfo:/ blob/upload [INFO] WARN [ExceptionHandler]执行POST/blob/upload失败[INFO] org.jboss.resteasy.spi.UnsupportedMediaTypeException:无法在org中使用内容类型[INFO]. jboss.resteasy.core.registry.Segment.match(Segment.java:117)[INFO]位于org.jboss的org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:33)[INFO]. resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:327)[INFO]位于org.jboss.resteasy的org.jboss.resteasy.core.registry.SimpleSegment.matchSimple(SimpleSegment.java:44)[INFO]. core.registry.RootSegment.matchChildren(RootSegment.java:327)[INFO] at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:
该客户端的JAX-RS接口应该是什么?
更新:
这里需要注意的一点是客户端代码已经编译和签名,我不能只是改变它.
启动一个新项目以创建一个宁静的Web服务,该服务要求Kerberos(Active Directory)对呼叫者进行身份验证.
Web服务将由Tomcat托管,我计划将JAX-RS与JAAS一起使用,但我没有找到关于此的更多信息,是否有人有关于此工作的信息或经验?
我应该看一下Spring和Spring Security吗?
如何为POST创建泽西单元测试?
如何添加帖子参数?
对于GET来说很简单(https://jersey.java.net/documentation/latest/test-framework.html):
@Test
public void test() {
final String hello = target("hello").request().get(String.class);
assertEquals("Hello World!", hello);
}
Run Code Online (Sandbox Code Playgroud)
对于帖子来说,它更加分散.我设法得到了响应对象,但是如何获得实际的响应对象(String)?
@Test
public void test() {
Response r = target("hello").request().post(Entity.json("test"));
System.out.println(r.toString());
}
Run Code Online (Sandbox Code Playgroud)
结果: InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:9998/hello, status=200, reason=OK}}
我发现这个问题的答案相互矛盾,但我没能成功运行一个例子.
JAX-RS是否可以使用带注释的Servlet 3.0(特别是Tomcat 7)实现,而无需实现另一个Servlet容器?
如果不是,请解释为什么本书中的以下引用是,不正确或我正在解释它,错误.
因为此示例在Java EE应用程序服务器或独立的Servlet 3.x容器中部署,所以我们只需要一个空的web.xml文件.服务器将检测到Application类在WAR中并自动部署它.(使用JAX-RS 2.0的RESTful Java,Bill Burke)
为了澄清什么,我也不需要.我曾成功地使用一个web.xml实现JAX-RS在Tomcat的7新泽西州的帮助,所以,我并不需要怎么做任何解释.另外,我完全清楚其他Java EE/Servlet容器(TomEE,Glassfish,Jersey,Websphere等等)都是开箱即用的JAX-RS.我只需要知道我是否正在追逐我的尾巴试图让Tomcat 7(Servlet 3.0)与JAX-RS一起工作而不添加Servlet容器和没有web.xml条目.
我是泽西岛的新手(2.22.2),所以请耐心等待.我正在创建一个与LDAP服务器连接的REST服务,用于存储,删除和检索用户数据.该服务通过执行加密/解密充当安全中介.
在使用REST服务之前必须进行相当多的初始化,并且我只想执行一次初始化(当应用程序部署在服务器上时).所以这项服务将作为单身人士运行.
如果有人能给我一些关于最佳方法的指示,我将不胜感激吗?谢谢!
大家好我是新来的休息和jax-rs所以我的问题是每个休息服务开始扩展该应用程序类和定义应用程序路径.现在我的问题是该应用程序类本身的生命周期是什么?这是一个例子:
import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends Application {}
Run Code Online (Sandbox Code Playgroud)
这是一个servlet吗?它总是活着吗?我怎么理解这堂课?它是一个cdi豆?服务器是否在每个请求上创建此类?