PostMapping with Parameters from URI - java

I am trying to build a spring boot #PostMapping method that gets it's parameters from the uri, something like this
http://localhost:8091/url/log?param1=asdf&param2=asd&param3=test
or like this
http://localhost:8091/url/log/msg/msg1/msg2
Is there a way to expand the code model from below to 3 parameters?
headers.setLocation(builder.path("/article/{param1}/{param2}/{param3}")
#PostMapping("article")
public ResponseEntity<Void> addArticle(#RequestBody ArticleInfo articleInfo, UriComponentsBuilder builder) {
Article article = new Article();
BeanUtils.copyProperties(articleInfo, article);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/article/{id}").buildAndExpand(article.getArticleId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}

If you simply want your API call to create a new Article, how about simply having a PostMapping to /article/new (for example) and then simply pass the new Article's parameters as RequestBody?
#PostMapping("article/new")
public ResponseEntity<Void> addArticle(#RequestBody Article article) {
// ...
}
Then as RequestBody you would have something like:
{ "param1": "value1", "param2": "value2", "param3": "value3" }
If you're just looking to include more PathVariable's to your api call, refer to #sovannarith cheav's answer
I hope this helps

Is there a way to expand the code model from below to 3 parameters? headers.setLocation(builder.path("/article/{param1}/{param2}/{param3}")
You can use #PathVariable, like below
#PostMapping("article/{param1}/{param2}/{param3}")
public ResponseEntity<Void> addArticle(#PathVariable("param1") String param1, #PathVariable("param3") String param3, #PathVariable("param3") String param3) {
//enter code here
}

Related

Using BodyInserters to pass parameter with webClient

There is my code.
public Mono<RespDto> function(TransReqDto1 reqDto1, TransReqDto2 reqDto2, String token) {
MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("TransReqDto1", reqDto1);
builder.part("TransReqDto2", reqDto2);
MultiValueMap<String, HttpEntity<?>> parts = builder.build();
LinkedMultiValueMap map = new LinkedMultiValueMap();
map.add("TransReqDto1", reqDto1);
map.add("TransReqDto2", reqDto2);
return
client.post()
.uri("/api")
.body(BodyInserters.fromValue(reqDto1))
.headers(h -> h.setBearerAuth(token.split(" ")[1]))
.retrieve()
.bodyToMono(RespDto.class);
}
My probelm is that I need to send both reqDto1 & reqDto2. I've successfully sent reqDto1 with the code above but I can't figure out a way to send two objects.
Tried MultipartBodybuild and MultiValueMap but both are returning error from the target API. Please give me some hints!! Thank you
Here is the API I am trying to call!
#PostMapping("")
#ApiOperation(value = "test", notes = "test")
public Mono<?> transPost(#Valid #RequestBody TransReqDto1 reqDto1,
#Valid #RequestBody TransReqDto2 reqDto2) {
return testService.function(reqDto1, reqDto2);
}
You cannot use two #RequestBody. It can bind to a single object only. The expected way to do that is to create a wrapper DTO containing all the relevant data:
public class TransReqDto {
private TransReqDto1 transReqDto1;
private TransReqDto2 transReqDto2;
//...
}

Return value from asynchonous running method to callback URL

I want to trigger a method to create a ArrayList and return this list to a callback-URL. This method can take a while until the data has been generated so it should be an asynchronous running method.
So I got a few questions:
What's a callback-URL? I assume it's the URL the method is returning the value to?
How to returna value to a callback-URL?
How to access the value after it has been returned?
#Async
#GetMapping("/createcustomer/{start}/{end}"){
public ArrayList<Customer> createCustomer(#PathVariable int start,
#PathVariable int end) {
//code to generate random data
return arrayList;
}
In order to call your handler method you can use javascript code on your front-end side. Here is an example how you can do it with axios library:
axios.get('createcustomer/something/something')
.then(function (response) {
console.log(response);
// response.data will contain your json with returned list content
})
.catch(function (error) {
console.log(error);
});
You will also need to modify your hanlder method like this:
#ReponseBody
#GetMapping("createcustomer/{start}/{end}"){
public ArrayList<Customer> createCustomer(#PathVariable int start,
#PathVariable int end) {
//code to generate random data
return arrayList;
}
The #ResponseBody annotation will cause the ArrayList to be returned as json in the body of the response. Alternatively, you can change #Controller class annotation to #RestController. It will make all your handler methods in this class to return back to the client.
This way, the request will be totally asynchronous and you can access it in your front-end code as soon as it's available.
Edit:
If you want to send this list somewhere else using callback-url, you can use RestTemplate to send post request with list as body. You can use something similiar to this code inside your handler method:
String callback-URL = "http://my.server.com/customerListEndpoint";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> entity = new HttpEntity<>(headers);
restTemplate.exchange(callback-URL,HttpMethod.POST,entity,ArrayList.class);
You need an endpoint that can be mapped with post requests to callback-URL. Something like:
#PostMapping("/customerListEndpoint"){
public void createCustomer(#RequestBody ArrayList<Customer> arrayList) {
// do what you want with arrayList here
}
Also, be sure to configure the RestTemplate bean in some class anottated with #Configuration:
#Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setOutputStreaming(false);
RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
restTemplate.getMessageConverters()
.add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}

Simple restful JSON POST with java as server and jquery as client

Before I ask my question I have to say that I have read more than 20 questions and articles about this problem and none of them could solve it.
My problem is I have a restful server in java like this:
#RequestMapping (value = "/downloadByCode", method = RequestMethod.POST)
#ResponseBody
public void downloadByCode(#RequestBody final String stringRequest, final HttpServletResponse response)
{
try
{
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonRequest = objectMapper.readValue(stringRequest, JsonNode.class);
// ...
// some processings here to create the result
// ....
final ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(result);
// Flush the result
outputStream.flush();
}
catch (final Exception exception)
{
LOG.debug("Exception Thrown [downloadByCode]", exception);
}
}
And I have tried different ways to send a json to this server with jquery (but all of them create errors):
$.ajax({
url:"/downloadByCode",
type:"POST",
data: JSON.stringify({"name":"value"}) });
415 "errors message" : "Content type 'application/x-www-form
urlencoded;charset=UTF-8' not supported", "type" :
"HttpMediaTypeNotSupportedError"
So I tried to fix it by adding contentType:
$.ajax({
url:"/downloadByCode",
contentType:"application/json",
type:"POST",
data: JSON.stringify({"name":"value"}) });
400 "errors message" : "Could not instantiate JAXBContext for class
[class java.lang.String]: null; nested exception is
javax.xml.bind.JAXBException\n - with linked
exception:\n[java.lang.NullPointerException", "type"
:"HttpMessageConversionError"
I tried to send json object directly instead of JSON.stringify and it gives the same 400 error.
I tried to add different consumes to the #requestMapping but still no luck.
I tried to define my own class instead of JsonNode but it does not change anything.
Any ideas?
Please try to create new class :
public class InputData{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
Then
public void downloadByCode(#RequestBody InputData stringRequest, final HttpServletResponse response)
And
$.ajax({
url:"/downloadByCode",
contentType:"application/json",
type:"POST",
data: JSON.stringify({"name":"value"}) });
try #RequestBody final Map<String, String> stringRequest
also you will need consumes = "application/json" on the #RequestMapping because you have that in your AJAX call
You will get 400 if spring doesn't like the format in which you send your ajax - I've had so much trouble with this in the past and it seems better to just ignore header types and content types unless necessary
You might try sending your response back as a ResponseEntity instead of using the HttpServletResponse directly. My hunch is that second argument, the HttpServletRequest argument, is what is causing the problem. I've never used that. I've always send my response back using the spring mvc api.
With Jersey api you can try just:
#POST
public void downloadByCode(String stringRequest)
and I think you'll find the body of your post in stringRequest.
You can take request body as string with usage of org.springframework.http.HttpEntity<String> as request type, here is example with your code as base:
#RequestMapping (value = "/downloadByCode", method = RequestMethod.POST)
#ResponseBody
public void downloadByCode(final HttpEntity<String> request, final HttpServletResponse response)
{
try
{
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonRequest = objectMapper.readValue(request.getBody(), JsonNode.class);
// ...
// some processings here to create the result
// ....
final ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(result);
// Flush the result
outputStream.flush();
}
catch (final Exception exception)
{
LOG.debug("Exception Thrown [downloadByCode]", exception);
}
}
But maybe it will be better to use also String as return type, if you are planning to return result as string value, like this:
#RequestMapping (value = "/downloadByCode", method = RequestMethod.POST)
#ResponseBody
public String downloadByCode(HttpEntity<String> request) {
String requestBody = request.getBody();
String result;
// ...
// some processings here to create the result text
// ....
return result;
}
I made simple application using Spring Boot with usage of proposed solutions using HttpEntity and also additional example of usage POJO, to run application you need to have Maven and JDK >= 1.7.
#clonning repository with sample
git clone git#github.com:mind-blowing/samples.git
#change current folder to sample application root
cd samples/spring-boot/rest-handler-for-plain-text
#running application using maven spring-boot plugin
mvn spring-boot:run
After application will be started you can open http://localhost:8080 and you will see html page with simple usage of JQuery to send POST requests, text of request and response will visible on html page, in controller I added two handlers, first with usage of HttpEntity and second with usage of POJO.
Controller: SampleRestController.java
HTML page: index.html
Project: https://github.com/mind-blowing/samples/tree/master/spring-boot/rest-handler-for-plain-text
First of all If you are using maven you should add dependency for jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
or you can download the jar and put it in our project class path (you can use other mapper as well)
then you should create a model or DTO class where you can map your json
public class Data{
private String name;
pubilc Data(){}
//getter and setter
}
THEN you controller
#RequestMapping (value = "/downloadByCode", method = RequestMethod.POST)
#ResponseBody
public Data downloadByCode(#RequestBody final Data data, final HttpServletResponse response)
{
//your code
return data;
}
AJAX CALL
$.ajax({
url:"/downloadByCode",
contentType:"application/json",
type:"POST",
data: JSON.stringify({"name":"value"}) });
(Optional)You can override behavior by telling object mapper not to fail on missing properties by defining the bean as follows:
#Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
return converter;
}
http://websystique.com/springmvc/spring-mvc-requestbody-responsebody-example/
Looking at your errors, it's clear that you have configured 'Jaxb2RootElementHttpMessageConverter' or similar XML converter in your spring configuration. And since you have registerned an XML converter, the #RequestBody and #ResponseBody work based on the registered message converters.
So, to solve your problem, go with a JSON message converter such as 'MappingJacksonHttpMessageConverter'. Once you register a JSON message converter, create a bean class to hold your json data and use it with RequestBody as below:
// It has to meet the json structure you are mapping it with
public class YourInputData {
//properties with getters and setters
}
Update 1:
Since you have defined multiple message converters, Spring tries to use the first one available by default. In order to use specific message converter(in this case Jackson converter), you should specify 'Accept' header from client like below:
$.ajax({
headers: {
"Accept" : "application/json; charset=utf-8",
"Content-Type": "application/json; charset=utf-8"
}
data: "data",
success : function(response) {
...
} })
The final answer is a combination of a number of answers/comments in this question that I am going to summarize them here:
1- You have to make sure you have an appropriate json converter in your spring config such as MappingJacksonHttpMessageConverter (credits to #java Anto)
2- You have to create a POJO class with same structure as your json object (see #Vinh Vo answer)
3- Your POJO class cannot be an inline class unless it is a static class. It means it should have its own java file or it should be static. (credits to #NTyler)
4- Your POJO class can miss parts of your json object if you set it appropriately in your object mapper (see #Aman Tuladhar answer)
5- Your ajax call requires contentType:"application/json", and you should send your data with JSON.stringify
Here is the Final code that is working perfectly:
public static class InputData
{
private String name
public String getName()
{
return name;
}
public void setName(final String name
{
this.name = name;
}
}
#RequestMapping(value = "/downloadByCode", method = RequestMethod.POST)
#ResponseBody
public void downloadByCode(#RequestBody final InputData request, final HttpServletResponse response)
{
try
{
String codes = request.getName();
// ...
// some processings here to create the result
// ....
final ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(result);
// Flush the result
outputStream.flush();
}
catch (final Exception exception)
{
LOG.debug("Exception Thrown [downloadByCode]", exception);
}
}
And it is the jquery Ajax request:
$.ajax({
url:"/downloadByCode",
contentType:"application/json",
type:"POST",
data: JSON.stringify({"name":"value"}) });
Delete the #ResponseBody on your downloadByCode method
Change your method downloadByCode() return type to String and then return the String
Response body will automatically convert the returned String to JSON and then use the data appropriately
I am not that well versed with java but as much as I know your java code must be something like this.
public class downloadByCode{
#GET
#Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response downloadByCode(#QueryParam("paramater1") final String parameter 1, #Context HttpServletRequest httpRequest) {
If this not helps you can keep you code somewhere and share it.

How to pass two query parameters in URL

In this example, the URL for a service has the form /projection/projectionId:
#Stateless
#Path("projection")
public class ProjectionManager {
#Inject
private ProjectionDAO projectionDAO;
#Inject
private UserContext userContext;
#GET
#Path("{projectionId}")
#Produces("application/json")
public String places(#PathParam("projectionId") String projectionId) {
return projectionDAO.findById(Long.parseLong(projectionId)).getPlaces().toString();
}}
How can I pass two (or more) query parameters to access the service using this code:
#PUT
#Path("/buy")
public Response buyTicket(#QueryParam("projectionId") String projectionId, #QueryParam("place") String place) {
Projection projection = projectionDAO.findById(Long.parseLong(projectionId));
if(projection != null) {
projectionDAO.buyTicket(projection, userContext.getCurrentUser(), Integer.parseInt(place));
}
return Response.noContent().build();
}
/buy?projectionId=value1&place=value2
Take a look at https://en.wikipedia.org/wiki/Query_string for further information. And since it is HTTP PUT you cannot simply open that URL in your browser, you can write some simple REST client or use browser extension like Postman in Chrome.
Query parameter is the thing after the ? in the URI, while path parameter is the parametrer before the ? in the URI.
If you need two inputs to your method, you can go with any combination of query param and path param => four combinations
It's a good convention that path params should denote some kind of identity of the resource, because it's part of it's address, while query params more some form/shape/filtering of the response.
In your case, I'd encode both params as path parameters, so the code would look like this:
#PUT
#Path("/buy/{projectionId}/place/{place}")
public Response buyTicket(#PathParam("projectionId") String projectionId, #PathParam("place") String place){
Projection projection = projectionDAO.findById(Long.parseLong(projectionId));
if(projection != null){
projectionDAO.buyTicket(projection, userContext.getCurrentUser(), Integer.parseInt(place));
}
return Response.noContent().build();
}
The URL would look like:
${host}/buy/1337/place/42
Thanks for your input guys, I have fixed it.
It looks like I had to add the path parameter to the additional parameters, and pass additional parameters on the request, instead of the path parameter. Code looks as below,
it('should get a customer, searches with a customer name', (done) => {
var pathParams = {};
var body = {};
var additionalParams = {
queryParams: {
name: 'Ominathi'
}
};
//apigClient.invokeApi(pathParams, '/customer', 'GET', queryParams, body)
apigClient.invokeApi(pathParams, '/customer', 'GET', additionalParams, body)
.then(response => {
expect(response.status).toBe(200);
done();
})
.catch(err => {
fail(err);
done();
});
});
Thanks.
Ref: https://www.npmjs.com/package/aws-api-gateway-client

Is it possible to set ETags using JAX-RS without resorting to Response objects?

In one of the few questions (with answers) I have found on SO regarding JAX-RS and caching, the answer to generating ETags (for caching) is by setting some values on the Response object. As in the following:
#GET
#Path("/person/{id}")
public Response getPerson(#PathParam("id") String name, #Context Request request){
Person person = _dao.getPerson(name);
if (person == null) {
return Response.noContent().build();
}
EntityTag eTag = new EntityTag(person.getUUID() + "-" + person.getVersion());
CacheControl cc = new CacheControl();
cc.setMaxAge(600);
ResponseBuilder builder = request.evaluatePreconditions(person.getUpdated(), eTag);
if (builder == null) {
builder = Response.ok(person);
}
return builder.cacheControl(cc).lastModified(person.getUpdated()).build();
}
The problem is that will not work for us, since we use the same methods for both SOAP and REST services, by annotating the methods with #WebMethod (SOAP), #GET (and whatever else we might need to expose the service). The previous service would look like this to us (excluding the creation of headers):
#WebMethod
#GET
#Path("/person/{id}")
public Person getPerson(#WebParam(name="id") #PathParam("id") String name){
return _dao.getPerson(name);
}
Is there any way - through some extra configuration - of setting those headers? This is the first time I have found that using Response objects actually has some benefit over just auto-conversion ...
We are using Apache CXF.
Yes you might be able to use interceptors to achieve this if you could generate the E-tag AFTER you create your response object.
public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
public MyInterceptor () {
super(Phase.MARSHAL);
}
public final void handleMessage(Message message) {
MultivaluedMap<String, Object> headers = (MetadataMap<String, Object>) message.get(Message.PROTOCOL_HEADERS);
if (headers == null) {
headers = new MetadataMap<String, Object>();
}
//generate E-tag here
String etag = getEtag();
//
String cc = 600;
headers.add("E-Tag", etag);
headers.add("Cache-Control", cc);
message.put(Message.PROTOCOL_HEADERS, headers);
}
}
If that way isn't viable, I would use the original solution that you posted, and just add your Person entity to the builder:
Person p = _dao.getPerson(name);
return builder.entity(p).cacheControl(cc).lastModified(person.getUpdated()).build();
or it can be as simple as sending back an "error" code... depending on what you want to do.
#Path("/{id}")
#GET
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public ProductSearchResultBean getProductById(#PathParam("id") Integer productId, #QueryParam("expand") List<String> expand, #Context HttpServletRequest request, #Context HttpServletResponse response) throws IOException {
ProductSearchResultBean productDetail = loadProductDetail(productId, expand);
EntityTag etag = new EntityTag(((Integer)(productDetail.toString().hashCode())).toString());
String otherEtag = request.getHeader("ETag");
if(etag.getValue().equals(otherEtag)){
response.sendError(304, "not Modified");
}
response.addHeader("ETag", etag.getValue());
return productDetail;
}
That's how I tackled the issure anyway. Good luck! (Use Spring MVC instead.... there's an out of the box filter that does EVERYTHING for you... even making a good ETag :) )
You might consider using a Response Filter for that. I developed a smell library doing exactly what you are looking for: https://github.com/tobilarscheid/jaxrs-etag-filter

Categories

Resources