Define Enunciate response of Map<String, String> - java

The closest response data type description I can get is
map of object (JSON)
with a response example of
...
when I use the annotation
#TypeHint(Map.class)
Ideally I need to specify a response type of Map<String, String>, HashMap<String, String>, or something that would provide a response data type that makes sense and a response example similar to
{
"...": "...",
"...": "..."
}

Found a solution using the #requestExample JavaDoc Tag detailed in the Enunciate documentation.
For example
#responseExample application/json {"..." : "..."}
Gives a proper response example of
{
"...": "..."
}
Using the #TypeHint(Object.class) annotation produces a response data type of object (JSON), which technically makes sense, so this solution is sufficient.

Related

Make Jackson interpret single value JSON

I'm looking for a solution to this problem and I can not find anything.
I have this JSON response from an HttpURLConnection:
"OK"
At first look I thought this kind of response was not correct because I always saw some key -> value pair JSON. After i read that the JSONs can take this form I learned :
And https://jsonlint.com/ give me a Valid JSON response if a try to validate "OK"so i think it's correct. And now my question is: How I can map a Java class (called RestResponse) with this JSON response in Jackson?
I try this but not work give me JsonMappingException: no single-String constructor/factory method :
final String json = "OK";
final ObjectMapper mapper = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
final RestResponse response = mapper.readValue(json, RestResponse.class);
I do not even know where to start with my RestResponse java class because it has no attributes that can mark with Jackson annotation like #JsonProperty.
Does anyone know why Jackson does not take this response case into consideration?
Or is there simply another method?
Thank you guys.
Your RestResponse will need a constructor that takes a single string. Annotate the constructor with #JsonCreator.

How to POST/PUT on a Map<String,Entity> collection association

Maybe a dumb question but how to update the content of a typed Map collection association of an exposed entity with Spring Data Rest using POST/PUT requests ?
I know how to POST/PUT on List or Set associations using Content-Type: text/uri-list but i don't with key/value Map<>
EDIT
I tried to send a ressource URI on the body of a PUT request with Content-Type: text/uri-list on the association endpoint. I gave no error, but it replaced all the previous content of the Map<> by this :
{
self: { ENTITY content}
}
So it seems to be supported (a side effect ?). It named the key self.
I don't know how to specify the key.
#WebService
#Path("/test")
public class TestResource {
#GET
#Produces(APPLICATION_JSON)
public HashMap<String, String> findAll() {
HashMap<String, String> test = new HashMap<>();
test.put("key1", "value1");
test.put("key2", "value2");
return test;
}
This produces JSON at the front end:
{"key2":"value2","key1":"value1"}
However this does use application/json content type which may not be what you are after?

Read Map entity in JAX-RS client

I have a webservice which returns a "Map", I am trying to read this object from the Response(javax.ws.rs.core).
something like this:
ex : Map<String, Object> temp = response.readEntity(Map.class)
but this doesn't seem to work.
My question is how do I read a Map entity from a response object?
Found a way to read the Map entity from response.I guess I needed to provide the implementation class for Map.
response.readEntity(new GenericType<HashMap<String, Object>>() { });

Jersey, JSR 303 validation - custom "path" and "invalidValue" in ValidationError

I am using Jersey (JAX-RS) and I'm trying to implement a validation. I have a problem with a response returned by my application when a validation error occurs. Now the response looks like this:
[{
"message": "Custom message",
"messageTemplate": "{custom.message.template}",
"path": "SomeJerseyResource.resourceMethod.arg0.names[0]",
"invalidValue":"[value1, value2]"
}]
where "SomeJerseyResourceClass.resourceMethod" is a JAX-RS resource:
public class SomeJerseyResource {
#POST
#Path("/path")
public Response resourceMethod(#Valid RequestModel request) {
/** method body **/
}
}
and validation constraint is assigned to a getter in RequestModel:
public class RequestModel {
private List<String> names = new ArrayList<>();
#MyConstraint
public List<String> getNames() {
return tags;
}
}
I have a custom ConstraintValidator, where I validate each element of that List.
Problem
I don't want to include resource and method name in "path" field of the response. Instead of
SomeJerseyResource.resourceMethod.arg0.names[0] I want arg0.names[0] only. Client doesn't know about server classes and methods, and he wouldn't be able to properly assign errors to fields when he receives response like that.
I want to customize "invalidValue" field of a response. More specifically, to have only invalid element value, not the whole list in that field.
I didn't find any easy way to do that. Do you have any ideas?
You can just write an ExceptionMapper<ConstraintViolationException> to return the Response of your liking. Jersey uses an ExceptionMapper<ViolationException>. ConstraintViolationException extends from ViolationException, so you're mapper is more specific, and would take precedence in the choosing of the mapper. Jersey's mapper, returns the response as a ValidationError, that's why the body is how it is. But you can make it whatever you want.
If you just want the invalidValue list, then just iterate through the ConstraintViolations from ContraintViolationException.getConstraintViolations(), and get the invalidValue from the ConstraintViolation.

How can I get values from #GET using #QueryParam("list") final List<MyDTO> listDTO

I have an service that should receive a List<Object>, in my case, FaturamentoDTO... ex:
#GET
#Path(value="/teste")
#Produces(MediaType.APPLICATION_JSON)
public List<FaturamentoDTO> teste(#QueryParam("list") final List<FaturamentoDTO> listFatsDTO) {
for (FaturamentoDTO f : listFatsDTO) {
// do my stuff...
}
return listFatsDTO;
}
So my question is, how can I send and receive the values.
JAX-RS specification says:
The following types are supported:
1 Primitive Types
2 Types that have a constructor that accepts a single String argument.
3 Types that have a static method named valueOf with a single String argument.
4 List, Set, or SortedSet where T satisfies 2 or 3 above.
But even with the constructor I can't get the values.
If you are sending anything other than simple strings I would recommend using a POST with an appropriate request body. However it must be possible with a GET.
How does your client send the request?
Your client should send a request corresponding to:
GET http://example.com/services/teste?list=item1&list=item2&list=item3
Query parameters don't have any specific support for complex data structures, so you are going to need to implement that yourself. I was facing something similar, and ended up using JSON representations of the data as the query parameter values. For example (note that the JSON should be URL encoded, but I left that out to make it readable)
http://service?item={"foo" : "value1", "bar" : "value2"}&item={"foo" : "value3", "bar" : "value4"}
You could then write a ParamConverter<T> to unmarshal the JSON into your FaturamentoDTO. I am using JAXB/MOXy, but this could be done using your JSON handling library of choice.

Categories

Resources