相关疑难解决方法(0)

How to handle CORS using JAX-RS with Jersey

I'm developing a java script client application, in server-side I need to handle CORS, all the services I had written in JAX-RS with JERSEY. My code:

@CrossOriginResourceSharing(allowAllOrigins = true)
@GET
@Path("/readOthersCalendar")
@Produces("application/json")
public Response readOthersCalendar(String dataJson) throws Exception {  
     //my code. Edited by gimbal2 to fix formatting
     return Response.status(status).entity(jsonResponse).header("Access-Control-Allow-Origin", "*").build();
}
Run Code Online (Sandbox Code Playgroud)

As of now, i'm getting error No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access."

Please assist me with this. …

java rest jax-rs jersey cors

63
推荐指数
3
解决办法
7万
查看次数

文件上传以及Jersey restful Web服务中的其他对象

我想通过上传图像和员工数据来在系统中创建员工信息.我能够使用球衣进行不同的休息呼叫.但我希望在一次休息电话中实现.我提供下面的结构.请帮我解决这方面的问题.

@POST
@Path("/upload2")
@Consumes({MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response uploadFileWithData(
        @FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
        Employee emp) {

//..... business login

}
Run Code Online (Sandbox Code Playgroud)

每当我想要做的时候,我都会在Chrome邮递员中出错.我的Employee json的简单结构如下所示.

{
    "Name": "John",
    "Age": 23,
    "Email": "john@gmail.com",
    "Adrs": {
        "DoorNo": "12-A",
        "Street": "Street-11",
        "City": "Bangalore",
        "Country": "Karnataka"
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我可以通过进行两次不同的调用来实现,但我希望在一次休息调用中实现,这样我就可以接收文件以及员工的实际数据.

请求您在这方面提供帮助.

java multipartform-data jax-rs jersey postman

61
推荐指数
1
解决办法
9万
查看次数

Jersey RESTful Web服务gradle设置

我坚持使用jersey库为RESTful Web服务创建gradle项目.项目配置应该能够在jetty应用程序服务器中启动服务.我已经找到了一个资源:https://github.com/ziroby/jetty-gradle-hello-world

我的解决方案的问题是,它使用了过时的球衣版本.我至少需要2版(最新的2.14版).我试图在maven central上搜索新版本,但在版本2中,很多工件名称发生了变化,我无法正确配置它.

编辑:我在项目中并不特别需要Jetty服务器.它可以是任何应用程序服务器,适用于测试和调试我的应用程序.我也在生产中使用码头,所以使用码头会很不错.

编辑 :(通过peeskillet) - 链接代码

建立

apply plugin: 'java'
apply plugin: 'jetty'

repositories {
    mavenCentral()
}
dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    testCompile 'com.sun.jersey:jersey-client:1.17.1'
    testCompile 'com.sun.jersey:jersey-core:1.17.1'
    compile 'com.sun.jersey:jersey-core:1.17.1'
    compile 'com.sun.jersey:jersey-server:1.17.1'
    compile 'com.sun.jersey:jersey-servlet:1.17.1'
}
test {
    exclude '**/*IntegrationTest*'
}

task integrationTest(type: Test) {
    include '**/*IntegrationTest*'
    doFirst {
        jettyRun.httpPort = 8080    // Port for test
        jettyRun.daemon = true
        jettyRun.execute()
    }
    doLast {
        jettyStop.stopPort = 8091   // Port for stop signal
        jettyStop.stopKey = 'stopKey'
        jettyStop.execute()
    } …
Run Code Online (Sandbox Code Playgroud)

java rest jersey gradle jersey-2.0

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

以编程方式在jersey中注册提供程序,实现exceptionmapper

如何以编程方式在jersey中注册我的提供程序,该程序实现了jersey API提供的Exceptionmapper?我不想使用@Provider注释并希望使用ResourceConfig注册提供程序,我该怎么做?

例如:

public class MyProvider implements ExceptionMapper<WebApplicationException> extends ResourceConfig {

     public MyProvider() {
        final Resource.Builder resourceBuilder = Resource.builder();
        resourceBuilder.path("helloworld");

        final ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod("GET");
        methodBuilder.produces(MediaType.TEXT_PLAIN_TYPE)
                .handledBy(new Inflector<ContainerRequestContext, String>() {

            @Override
            public String apply(ContainerRequestContext containerRequestContext) {
                return "Hello World!";
            }
        });

        final Resource resource = resourceBuilder.build();
        registerResources(resource);
    }

    @Override
    public Response toResponse(WebApplicationException ex) {
        String trace = Exceptions.getStackTraceAsString(ex);
        return Response.status(500).entity(trace).type("text/plain").build();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?

java rest web-services jersey

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

ResourceConfig 和应用程序

使用 ResourceConfig 比 Application 有什么优势(因为 ResourceConfig 扩展了 Application)。

使用资源配置

   @ApplicationPath("/")
    public class MyApplication extends ResourceConfig {
    public MyApplication() {
    super(MultiPartResource.class, MultiPartResource.class, MultiPartFeature.class);
    }
    }
Run Code Online (Sandbox Code Playgroud)

使用应用程序

public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources and features
classes.add(MultiPartFeature.class);
classes.add(MultiPartResource.class);
classes.add(LoggingFilter.class);
return classes;
}
}
Run Code Online (Sandbox Code Playgroud)

多部分表单数据的Jersey 2 注入源后,@ Arul Dhesiaseelan 回答将 MultiPartFeature 添加到两者以在服务器端启用该功能。有人可以解释一下。

rest web-services jersey

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

如何使用Jersey作为没有web.xml的JAX-RS实现?

我已经读过java EE6中的web.xml是可选的.所以没有web.xml,我如何告诉应用服务器使用Jersey作为JAX-RS规范的实现?

java web-services jax-rs java-ee

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

如何验证Jersey中的用户

我正在使用Jersey在Java中编写RESTful应用程序,我需要对用户进行身份验证.我知道我可以使用注释@RolesAllowed在资源中指定角色,但我无法理解用户如何与特定角色相关联.客户端以这种方式发送用户名和密码

    HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(user, password);
    Client client = ClientBuilder.newClient();
    client.register(feature);
    WebTarget target = client.target(baseUrl).path(urlString);
    Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
    Response response = invocationBuilder.get();
Run Code Online (Sandbox Code Playgroud)

假设某些方法只能由超级用户和其他用户使用,当客户端发送用户名和密码时,如何区分它们?

java rest jersey

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