dimanche 1 mars 2015

How to POST complex Collection of objects from HTML Form to Jersey method

I'm struggling with Jersey parameter conversion. I've already tried all similar Q&A here in SO but nothing worked for me so far.


Here are my sources:



<!- HTML Form ->

<form action="/rest/0.1/object/save"
enctype="application/x-www-form-urlencoded"
method="POST">
<input type="text" name="default_name" />

<input type="hidden" name="object_translation[0][language_code]" value='en'>
<input type="text" name="object_translation[0][name]">

<input type="hidden" name="object_translation[1][language_code]" value="it">
<input type="text" name="object_translation[1][name]">

<input type="submit" />
</form>





//com.myobject.ws.MyResource.java
@POST
@Path("/save")
//@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response saveObject(@FormDataParam("object_translation") List<List<BaseTranslationElement>> object_translation
)
{
System.out.println(object_translation);

return Response.ok().build();
}





//BaseTranslationElement.java

package com.myobject.model;

import javax.ws.rs.ext.ParamConverter;

public class BaseTranslationElement //implements ParamConverter
{
int id;
String name;
Language language;

public BaseTranslationElement() {}

public BaseTranslationElement(String name, Language language)
{
this.name = name;
this.language = language;
}

public BaseTranslationElement(int id, String name, Language language)
{
this.id = id;
this.name = name;
this.language = language;
}

public static BaseTranslationElement valueOf(String toDecode)
{

return new BaseTranslationElement("name1", new Language("en", "LAAA"));
}

public int getId() { return id; }

public void setId(int id) { this.id = id; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public Language getLanguage() { return language; }

public void setLanguage(Language language) { this.language = language; }

/* @Override
public Object fromString(String value) { return null; }

@Override
public String toString(Object value) { return null; }*/
}

<!-- my pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://ift.tt/IH78KX"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<modelVersion>4.0.0</modelVersion>

<groupId>com.myobject</groupId>
<artifactId>my-ws</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>

<build>
<finalName>my-ws</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>



//Error in catalina.out during startup


INFORMAZIONI: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM' mar 01, 2015 4:32:34 PM com.sun.jersey.spi.inject.Errors processErrorMessages GRAVE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.myobject.ws.MyResource.saveObject(java.util.List) at parameter at index 0 SEVERE: Method, public javax.ws.rs.core.Response com.appappa.ws.MyResource.saveObject(java.util.List), annotated with GET of resource, class com.myobject.ws.MyResource, is not recognized as valid resource method.



Aucun commentaire:

Enregistrer un commentaire