문제

So i use Amdatu inside a Felix framework to create an OSGi enabled JSON Rest Service.

When i use @GET i get the id value as expected

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("file")
public String getFile(@QueryParam("id") String id) {
    System.out.println("id : "+id);
    return null;
}

When i use @POST FormParam is always null

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("file")
public String getFile(@FormParam("id") String id) {
       System.out.println("id : "+id);
       return null;
}

When i use @POST but with application JSON i always get the entire raw json and not the value.

I followed this video : http://www.amdatu.org/howto/createwebapp.html

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("file")
public String getFile(String id) throws Exception { 
    return id
}

I'm using the advanced rest client plugin for chrome to test the service.

Using libraries

org.amdatu.web.rest.jaxrs:1.0.4
org.amdatu.web.rest.wink:1.0.8
jackson-jaxrs:1.9.13
jackson-mapper-asl:1.9.13
jackson-core-asl:1.9.13

Update :

I had the depedencies in my maven bundle set to "provided" by changing them to "compile" MediaType.APPLICATION_FORM_URLENCODED now works.

But the MediaType.MULTIPART_FORM_DATA still doesnt.

During the post of the form my header is :

Content-Type: multipart/form-data

if i remove the @FormParam then the id is filled with :

id : --ARCFormBoundary5xbnwa6as8aor
Content-Disposition: form-data; name="id"

9
--ARCFormBoundary5xbnwa6as8aor--

the moment i add @FormParam the value is null.

도움이 되었습니까?

해결책

The JAX-RS specification doesn't say anything about how multipart/form-data should be handled. Different JAX-RS implementations have their own proprietary ways to deal with this. As far as I can find, Apache Wink (that we build on top of) doesn't support @FormParam for multipart. It does seems that there are some support types in Wink for multi part: http://wink.apache.org/documentation/1.1.1/html/7.8%20MultiPart.html but these classes are not exposed by the Amdatu Wink bundle, and I have never tried using this either.

A workaround I use myself is the example below. This is useful when multipart is used to upload files together with other form fields (this is why multipart is mostly used). I use Apache File Upload to parse the request and get access to both the uploaded files and form fields.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void test(@Context HttpServletRequest request) {
    ServletFileUpload uploader = new ServletFileUpload(new DiskFileItemFactory());
    try {
        List<FileItem> parseRequest = uploader.parseRequest(request);
        for (FileItem fileItem : parseRequest) {
            if (fileItem.isFormField()) {
                System.out.println(fileItem.getFieldName() + ": "
                        + fileItem.getString());
            }

        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
}

다른 팁

It looks like you're using the wrong Mediatype in @Consumes.

I created an example (see below) that accepts a parameter from a form, either by omitting the @Consumes completely, or setting it to MediaType.APPLICATION_FORM_URLENCODED.

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void test(@FormParam("id") String id) {
    System.out.println(id);
}

HTML

<form action="/agenda" method="post">
<input type="id" name="id"/>
<input type="submit" value="Test">
</form>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top