Please, help to customize web service JSON output format

50 views
Skip to first unread message

Сергей Хилков

unread,
Sep 29, 2021, 1:51:49 AM9/29/21
to Payara Forum
Hi,
I have been trying to change JSON output of my web service, 
but all JSON annotations,  JSON configurators are not working.
The result output of sending list of objects from two instances looks like this:

[[3,1,2,-1,"2021-09-20T08:55:31Z[UTC]",null],[4,1,2,-1,"2021-09-20T08:58:15Z[UTC]",null]]

It even doesnt contain field names( I have not found somebody had the same problem..)!
I tried to change at least date output format. I declare field with following annotations
@JsonbDateFormat("dd.MM.yyyy")
@JsonbProperty("dateAdd")
No results! 

I have tried to create custom JSONB config. I created class

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.config.PropertyNamingStrategy;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
public class JSONConfigurator implements ContextResolver
{

    @Override
    public Jsonb getContext(Class type)
    {
JsonbConfig config = new JsonbConfig().withDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", null).withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);;
return JsonbBuilder.newBuilder().
    withConfig(config).
    build();
    }
}

Then I tried to add custom config to my application:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
 * Configures JAX-RS for the application.
 * @author Juneau
 */
@ApplicationPath("resources")
public class JAXRSConfiguration extends Application 
{
      @Override
    public Set<Class<?>> getClasses() 
    {
        Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add(JSONConfigurator.class);
// resources.addAll(super.getClasses());
////        addRestResourceClasses(resources);
//        resources.add(JSONConfigurator.class);
        return resources;
    }
}

Application just stopped working, without any error in server logs....


Sergey K

unread,
Sep 29, 2021, 4:15:48 AM9/29/21
to Payara Forum
Update: I found that JSON without names is produced only for JPA objects...  How can I configure it??

среда, 29 сентября 2021 г. в 12:51:49 UTC+7, Sergey K:

Sergey K

unread,
Sep 29, 2021, 4:38:57 AM9/29/21
to Payara Forum
Annotations like @JsonbProperty("id")  also don't work with JPA objects.. 

среда, 29 сентября 2021 г. в 15:15:48 UTC+7, Sergey K:

Eduard Drenth

unread,
Sep 29, 2021, 6:12:56 AM9/29/21
to Sergey K, Payara Forum
You may consider using a construct like below (snippets...), though I think @JsonProperty should work if your annotated jpa objects didn't get transformed during processing.

private static final JsonbConfig jc = new JsonbConfig()
.withStrictIJSON(true);

private static final JsonGeneratorFactory FACTORY = Json.createGeneratorFactory(jc.getAsMap());

private static final JsonBuilderFactory BUILDER_FACTORY = Json.createBuilderFactory(jc.getAsMap());

try (JsonGenerator generator = FACTORY.createGenerator(outputStream);) {
generator.writeStartObject().writeStartArray("results");
JsonObjectBuilder objectBuilder = BUILDER_FACTORY.createObjectBuilder();
jsonbService.find(
query,
params, 0, -1, outClass)
.forEach(
o -> generator.write(mapper.map(objectBuilder, o))
);
generator.writeEnd().writeEnd();

public interface JsonMapper<T> {
String LEMMA = "lemma";
String POS = "pos";
JsonMapper<Synonym> synonymJsonMapper = new JsonMapper<Synonym>() {
@Override
public JsonObject map(JsonObjectBuilder builder, Synonym s) {
return builder
.add(LEMMA, s.getLemma().getForm())
.add(POS, s.getLemma().getPos())
.add("synonym", s.getForm())
.add("meaning", s.getMeaning())
.build();
}
};
}
--
You received this message because you are subscribed to the Google Groups "Payara Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to payara-forum...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/payara-forum/411d5c05-8fa7-42d5-965e-01bcd7dc8e90n%40googlegroups.com.

Sergey K

unread,
Sep 30, 2021, 6:45:40 AM9/30/21
to Payara Forum
Thank you,  Eduard!
A problem was in a way I fetched objects... Query used to return just list of Objects... I changed that and problem was solved.


среда, 29 сентября 2021 г. в 17:12:56 UTC+7, Eduard Drenth:
Reply all
Reply to author
Forward
0 new messages