I have written a REST service using jersey which maps a object-structure to a json-result, like:
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/orders/{id}")
public Order getById(#PathParam("id") String orderId) {
return orderService.getById(id); // Simplified
}
It works perfectly and returns the Order object structure as a JSON structure.
Now a (jersey) client accesses this REST service and gets those JSON structure back. It can parse it as a JSON structure, but it would be nice to map it back to (the server's) object structure.
But the server's objects contain many JPA-Annotations and other imports the client does not know about.
How can I share anyway my server object structure as (unannotated?) POJOs to my client? TIA!
Try a java generator from json https://javafromjson.dashingrocket.com/
Related
Background
We are consuming the API from a 3rd-party vendor
Problem Statement:
I am building a wrapper API around another API. The same exact JSON payload that I will be receiving from the client to the wrapper API will also be used to make an HTTP request to the original API.
Currently I'm converting JSON which deserializes to a String. Is this the right approach if the payload is just passing through the wrapper API to the original API? In other words, is the #RequestBody type String okay for my use case or do I still need to deserialize to a Java Object?
Use Case for wrapper
If multiple teams consumed the API from the 3rd-party vendor, all teams would have to make changes if we were to switch vendors. If we create a wrapper, only one team would have to make the changes. There is no processing in this wrapper.
Controller Code:
#RestController
#RequestMapping(value = FolderController.PATH, produces = MediaType.APPLICATION_JSON_VALUE)
public class PersonController(){
static final String PATH = "/person";
private final PersonService personService;
#Autowired
public PersonController(PersonService personService){
this.personService = personService
}
#PostMapping
#ResponseBody
public String createPerson(#RequestBody String requestBody){
return personService.createPerson(requestBody);
}
Whether you need to deserialize depends on what processing is necessary for your wrapper. If you just want to pass the request further without changing it, this should work.
However, if you just need to proxy the request, consider using Smiley's HTTP Proxy Servlet for this task.
Or if you are wrapping the API to implement security around it, then consider using Spring cloud gateway
It is always better to follow coding practices and convert the request body to a java POJO class instead of a string. Converting to POJO has several advantages :
You could provide additional validations for the request body.
Incase the request body changes or if there are any issues it could be identified easily.
Provides better control over the request structure.In future , if the API that you are consuming changes the request body structure, you could change it from your API instead of adapting it at every API that consumes your API.
I am trying to unit test a class that uses Jersey 2 Client + Moxy to call a REST service. I want to mock the response, which contains a large JSON object. The code is structured in such a way that I can override/mock the following method:
protected Response doPost(String path, Entity<?> entity) {
Invocation.Builder invocationBuilder = getRestInvocationBuilder(path);
Response response = invocationBuilder.post(entity);
return response;
}
I would like to somehow inject some example JSON data (ideally from a file) into the Response at this point, prior to readEntity() being called, so that I can test that the JSON data is correctly unmarshalled into the target object.
Is there any way to do this? Note that this is for unit testing and therefore I'm not interested in running a local server or other integration testing techniques.
I'm aware similar questions have been asked, but many seem out of date or have incomplete solutions. The closest solution suggested is to mock the readEntity() method of the Response, this will not work for me because it would involve creating an object of the desired type to return, rather than creating one from the example JSON data.
I am creating a Web Service that will handle incoming request properly (I knew, it is a description) I found some sample code which present SOAP WS and looks like:
#SoapAction("some fine url")
#ResponsePayload
public CertResponse getCert(#RequestPayload Cert param)
{...}
It takes Cert object from request and manages it properly. I want to do it in the REST approach and I changed this class to look like:
#RequestMapping(value="getCert", method = RequestMethod.POST)
#ResponseBody
public CertResponse getCert(#RequestBody Cert param)
{...}
But I have no idea how or even if it is possible to write a client that can send object through HTTP Post.
Can anyone give me a hint how can i send object Cert? Or if i have to stay with #SoapAction what should I do to make it work? I guess removing #Controller is not enough.
In SOAP approach, there is a well defined way to convert each class object to SOAP formatted XML. Thus, there is no effort.
If you will use RESTful approach, you have to describe how your Cert or CertResponse objects will be written to/read from the response/request.
Basically you have three options:
Use JSON or XML or plain String. Convert your Web service descriptor so that the request and response are one of those (JSON, XML, String). Then your getCert method should convert the request to Cert object, prepare your response as CertResponse object and convert it to an appropriate response type.
Leave your getCert method as is. But you need to specify "how Cert objects are read from request" and "how CertResponse objects are written to response" You need to define classes that extend interfaces MessageBodyReader and MessageBodyWriter. In these classes a similar conversion logic should be implemented that converts your objects to/from JSON or XML or String.
In any of these two options you need to implement two things : A method that converts JSON/XML/String to Cert, a method that converts CertResponse object to JSON/XML/String.
You can do a text based implementation, which parses/constructs the request/response by text processing and uses String class only. Or you can use some library such as JSON library with JSONObject class, or Java SAX or DOM based XML libraries that come with the Java bundle.
Check out for application frameworks such as Spring. They might provide ways to automatically convert your objects to JSON or XML, reducing the programming effort necessary.
Spring allows you to pass JSON objects from the client as a request parameters, it will convert them into your objects automatically. This discussion have some examples.
SOAP services are not really compatible with REST semantics. And it's not clear what SOAP framework do you use. Typically most SOAP frameworks offer you one way or another to generate a SOAP client code for you WSDL. You can check cxf.apache.org.
For REST services use something like Jersey or Spring MVC
I have to invoke a restful Web-Service from client side java class.
I need to pass HashMap, Strings and it must return me a list of beans.
I am using jersey restful web service
My REST service is like this:
#put
public List<MilestoneDetailsBean> getMPPReader(
#QueryParam("username") String username,
#QueryParam("projid") String projid,
#QueryParam("mppfile") File file,
#QueryParam("dbtemplate") Map<String,Integer> dbtemplate)
could some one help me with how could I:
assign values to these query parameters in my client side java code
what type of produces and consumes parameter I should put for my Web-Service
1) depends on how you create the query. QueryParams are those parts of an URL behind the ?: ?key=value&key2=value2
So what you could do is to just append the keys and values to the request URL. Remember to encode the values.
Like: http://mydomain/service?username=hage&projid=hello+world&mppfile=myfile.txt
Map is not usable for this. See here
2) Dont't know. Produces definitely depends on how you want to return the data (as xml, json, etc) and Consumes depends on what data you want to send to the server
Generally, for the client there exists a Jersey client API. Didn't use it yet, but you might look at it.
I've built up a Webservice in Java using Jersey.
The Webservice consumes XML and takes a POJO (CoResponse) as MethodParameters.
I.E.
#PUT
#Consumes(MediaType.APPLICATION_XML)
public CoResponse test(CoResponse obj){
//...do something....
return obj;
}
On Client side I would do a Put Request like this...
CoResponse rO = service.path("path")
.type(MediaType.APPLICATION_XML_TYPE)
.accept(MediaType.APPLICATION_XML)
.put(CoResponse.class, new CoResponse());
Actually everything is working fine in our environment. But now I would like to know what the xml-string sent to the Server looks like. The reason why is to use the webservice also in other environments by creating a custom Serializer / Deserializer (i.e. for windows mobile) that is compatible to our jersey webservice.
Is there a way of looking into the put method to see the final xmlstring? Or some other possibilities?
Use a LoggingFilter.
Just add it to your client:
client.add(new LoggingFilter(System.out));