标签: jax-rs

休息 - 如何获取呼叫者的IP地址

我正在编写Java Rest Web服务并需要调用者的IP地址.我以为我曾经在cookie中看到过这个,但现在我没有看到它.是否有一致的地方获取此信息?

我看到了一个使用"OperationalContext"来获取它的例子,但这不是在java中.

java rest web-services restful-authentication jax-rs

25
推荐指数
3
解决办法
4万
查看次数

JAX-RS中QueryParam和MatrixParam有什么区别?

JAX-RS @QueryParam@MatrixParam?之间的区别是什么?从文档中,queryparam和matrixparam都可以在特殊条件下定位一个资源.那么用例区别是什么?

PS:

Queryparam:

url ? key=value;

Matrixparam

url; key=value;

java jax-rs

25
推荐指数
2
解决办法
3万
查看次数

Java RestFull WebService:使用Jersey 2.3.1库的JAX-RS实现

我试图在JBoss jboss-eap-6.1 AS上运行一个简单的"Hallo World"应用程序Jersey 2.3.1 REST服务.在web.xml中我禁用了restEasy库.在部署期间,我收到错误:

JBWEB000289:Servlet com.sun.jersey.samples.helloworld.resources.MyApplication引发了load()异常:java.lang.NoSuchMethodError:javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

在POM我把这些依赖:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是我的web.xml,其中restEasy标签禁用:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>com.sun.jersey.samples.helloworld.resources.MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.sun.jersey.samples.helloworld.resources.MyApplication</param-value>
        </init-param>
           <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.providers</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.scan.resources</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>com.sun.jersey.samples.helloworld.resources.MyApplication</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)

我的资源配置java类:

package com.sun.jersey.samples.helloworld.resources;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends  ResourceConfig {   

     public MyApplication() {
            packages("com.sun.jersey.samples.helloworld.resources");
          //super(HelloWorldResource.class);

     }
}
Run Code Online (Sandbox Code Playgroud)

有人有任何想法解决它吗?罗伯托,提前谢谢

java jax-rs jersey jboss6.x

25
推荐指数
3
解决办法
6万
查看次数

Spring MVC REST不符合JAX-RS.有关系吗?

我在Spring MVC REST方面有很好的经验,并提供了几个可靠的项目.我的问题是关于JAX-RS合规性.这是否重要,因为Spring将继续存在,我无法预见(也没有理由)不得不在很短的时间内从Spring MVC REST转移到Jersy或任何其他JAX-RS.任何应该迫使我考虑使用JAX-RS实现而不是Spring MVC REST的东西?

java rest spring jax-rs spring-mvc

25
推荐指数
1
解决办法
2万
查看次数

设置JAX-RS 2.0 Client API的请求超时

我编写了简单的REST Web服务客户端类,它使用JAX-RS 2.0客户端API来发出REST请求.我试图弄清楚如何为每次调用设置请求超时.以下是请求的代码:

Client client = ClientBuilder.newBuilder().build();
WebTarget resourceTarget = client.target(restServiceUrl)
        .path("{regsysID}/{appointmentID}/")
        .resolveTemplate("regsysID", regSysId)
        .resolveTemplate("appointmentID", apptId);

Invocation invocation = resourceTarget.request(MediaType.APPLICATION_JSON).buildPut(null);
String createSessionJson = invocation.invoke(String.class);
Run Code Online (Sandbox Code Playgroud)

java rest jax-rs

25
推荐指数
4
解决办法
4万
查看次数

如何让客户端等待Java JAX-RS服务来阻止DOS

我遇到了一个Web服务的问题,用户试图通过循环随机ID来猜测应用程序ID.

坏请求来自随机IP,所以我不能只禁止他们的IP(除非我动态地做,但我还没有考虑过).

目前,当我检测到已经进行了10次不良应用ID尝试的客户端时,我将它们放在我的应用中的阻止列表中,并拒绝当天该IP的进一步请求.

我想尽量减少我的服务器需要做的工作量,因为坏客户端将继续发送1000个请求,即使它们被拒绝.我知道有动态防火墙解决方案,但现在想要在我的应用程序中轻松实现.目前我正在睡觉5秒以减少呼叫,但我想要做的只是不向客户端发送响应,因此它必须超时.

任何人都知道如何在JAX-RS中用Java做到这一点?

我的服务就像,

@Path("/api")
public class MyServer {

@GET
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/my-request")
public String myRequest(String type,
    @Context HttpServletRequest requestContext,
    @Context HttpServletResponse response) {
...
}
Run Code Online (Sandbox Code Playgroud)

请参阅: 如何阻止对Web API的黑客攻击/ DOS攻击

java rest web-services jax-rs ddos

25
推荐指数
3
解决办法
3727
查看次数

如何使用jax-rs上传多部分/表单文件?

(特别是RESTeasy)

对于单个文件来说,有一个方法签名是很好的:

public void upload(@FormParam("name") ..., @FormParam("file") file: InputStream)
... 
Run Code Online (Sandbox Code Playgroud)

可行?还是我在做梦?似乎并不那么简单.

jax-rs resteasy

24
推荐指数
1
解决办法
3万
查看次数

RESTful webservice:如何在java中设置头文件以接受Access-Control-Allow-Origin允许的XMLHttpRequest

我有一个RESTful webservice,它将返回字符串,它是用Java(JAX-WS)编写的.我的问题是当我使用以下URL向该webservice发送请求时:

http://localhost:8080/project/webservices/getlist/getListCustomers

在控制台中,它给出了以下错误消息:

XMLHttpRequest无法加载url Access-Control-Allow-Origin不允许使用origin localhost

我该如何处理这个问题?

Java代码:

@GET
@Path("/getsample")
public Response getMsg() { 
    String output = "Jersey say : " ;   
    return Response.status(200).entity(output).build();
}
Run Code Online (Sandbox Code Playgroud)

java rest jax-rs jax-ws cors

24
推荐指数
1
解决办法
8万
查看次数

尝试通过JAX-RS Web服务发送JSON对象,获取"找不到媒体类型= application/json的MessageBodyWriter"

我试图通过JAX-RS Web服务发送JSON对象.我的文件web.xml是:

<servlet>
 <description>JAX-RS Tools Generated - Do not modify</description>
 <servlet-name>JAX-RS Servlet</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>it.notifire</param-value>
  </init-param>
  <init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

模拟我要发送的对象的类是:

public class GPSCoordinate {

private float latitudine;
private float longitudine;

 public float getLatitudine() {
    return latitudine;
}
public void setLatitudine(float latitudine) {
     this.latitudine = latitudine;
}
public float getLongitudine() {
    return longitudine;
}
public void setLongitudine(float longitudine) {
    this.longitudine = longitudine;
}
}
Run Code Online (Sandbox Code Playgroud)

根类资源是:

@Path("position")
public class Position {

    @Context
private …
Run Code Online (Sandbox Code Playgroud)

java rest json web-services jax-rs

24
推荐指数
4
解决办法
6万
查看次数

如何从与Jersey的多部分表单中读取具有相同名称的多个(文件)输入?

我已经成功开发了一项服务,我在其中阅读在泽西岛以多部分形式上传的文件.这是我一直在做的非常简化的版本:

@POST
@Path("FileCollection")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {
    //handle the file
}
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但我得到了一个新的要求.除了我上传的文件外,我还要处理任意数量的资源.我们假设这些是图像文件.

我想我只是为客户端提供一个表单,其中包含一个文件输入,一个输入用于第一个图像,一个按钮允许向表单添加更多输入(使用AJAX或简单的JavaScript).

<form action="blahblahblah" method="post" enctype="multipart/form-data">
   <input type="file" name="file" />
   <input type="file" name="image" />
   <input type="button" value="add another image" />
   <input type="submit"  />
</form>
Run Code Online (Sandbox Code Playgroud)

因此,用户可以为表单添加更多图像输入,如下所示:

<form action="blahblahblah" method="post" enctype="multipart/form-data">
   <input type="file" name="file" />
   <input type="file" name="image" />
   <input type="file" name="image" />
   <input type="file" name="image" />
   <input type="button" value="add another image" />
   <input type="submit"  />
</form>
Run Code Online (Sandbox Code Playgroud)

我希望阅读与集合同名的字段非常简单.我在MVC .NET中使用文本输入成功完成了它,我认为在Jersey中不会更难.事实证明我错了.

找不到关于这个主题的教程,我开始尝试.

为了看看如何做到这一点,我把问题简化为简单的文本输入.

<form action="blahblabhblah" …
Run Code Online (Sandbox Code Playgroud)

java forms jax-rs multipart jersey

23
推荐指数
2
解决办法
2万
查看次数