English 中文(简体)
可否将POJO序列化成一种@GET方法?
原标题:Can RestEasy serialize a POJO for a @GET method?

我们使用雷特,并存在问题,难以称之为某种@GET方法。

如果该方法的接口只有简单的参数,就不存在问题。 注

@GET
@Path("/test/{myparam}")
public FacetQueryResultImpl testMethod(@PathParam("myparam")String myparam);

但是,如果我们试图将日本宇宙开发公司作为参数,那么它似乎无法把它编为查询参数。 注

@GET
@Path("/testGet")
public FacetQueryResultImpl testMethod(ParamPojo myparam);

@GET
@Path("/testGet")
public FacetQueryResultImpl testMethod(@QueryParam("myparam")ParamPojo myparam);

(与ParamPojo.java合著)

public class ParamPojo
{
    private String name;
    private String description;
    (...)
}

当我们尝试时,有时没有找到这些服务,有时我们获得“教育、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训、培训

Using @POST we are able to use a POJO has the parameter, but some of our methods don t modify anything on the server, and theref或e should use @GET.

A w或karound is to "explode" the ParamPojo, and use all of its properties as separated parameters f或 the method. But this removes the "Easy" part of "RestEasy", doesn t it?

最佳回答

You must use the @org.jboss.resteasy.annotations.Form annotation on your method parameter. http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html_single/#_Form

例:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import junit.framework.Assert;

import org.jboss.resteasy.annotations.Form;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.Test;

public class TestCase {
    @Path("/")
    public static class Service {

        @Path("")
        @GET
        public String get(@Form ValueObject vo){
            return vo.getParam();
        }
    }

    public static class ValueObject {
        @QueryParam("myparam")
        private String param;

        public String getParam() {
            return param;
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getRegistry().addSingletonResource(new Service());

        MockHttpRequest request = MockHttpRequest.get("/?myparam=somevalue");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        Assert.assertEquals("somevalue", response.getContentAsString());
    }
}
问题回答

对多重问题具有约束力 进入一个单一目标需要增加@ Form <POJO CLASS> as a debate in response means. 这对我们来说是好的。

@GET    
@Path("/")  
@Produces("application/json")
@Consumes("application/json")
public Response search(@Form CatalogSearchRequest reqObject) {
    System.out.println("Entered into service" + reqObject.getAttribute());
}

POJO 班级应当包含每个特性的@QueryParam(”),例如:

@QueryParam("pageSize")
public Integer pageSize;

@QueryParam("page")
public Integer page;

public Integer getPageSize() {
    return pageSize;
}

public void setPageSize(Integer pageSize) {
    this.pageSize = pageSize;
}

public Integer getPage() {
    return page;
}

public void setPage(Integer page) {
    this.page = page;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签