Spring mvc test case with string in request header and multipart file as a request parameter for a post request - spring-mvc-test

#Controller
#PostMapping("/hello/{studentName}")
public ResponseEntity<Void> method1(
#RequestMapping(value = "/upload/{studentName}", method = RequestMethod.POST)
#ResponseBody
public String saveAuto(
#PathVariable(value = "name") String name,` `
#RequestParam("file") MultipartFile myFile) {
}
}
Hi, I am new to unit test. can anyone please help me for writing test case using mockmvcbuilderrequest..
I tried this but getting 404
mockMvc.perform(MockMvcRequestBuilders.multipart("/hello/{zoneName}","com.example")
.file(file).accept(MediaType.MULTIPART_FORM_DATA_VALUE))

You have 2 options.
Change the rest path and put: "/hello/{studentName}", in this way the test will work as you have explained.
Leave the rest path "/upload/{studentName}" and change the uri in the test from "/hello/{studentName}" to "/upload/{studentName}".
I leave the way to execute the test, with the correction.
mockMvc.perform(
MockMvcRequestBuilders.multipart("/upload/{studentName}","Anu Shree")
.file(file)
.accept(MediaType.MULTIPART_FORM_DATA_VALUE)
)
I hope it helps

Related

How JAXB mapping is done in Spring boot SOAP webservices

I have a question on JAXB mapping using org.springframework.ws.server.endpoint.annotations.
I was able to generate Java domain object with provided *.xsd. The thing is after I define my endpoint with
#PayloadRoot, I have to wrap my request and response as below to successfully trigger the method and return a result:
#PayloadRoot( localPart = "PmtAuthAddRequest",
namespace = "http://*com/emb/webseries")
#ResponsePayload
public JAXBElement billPayment(#RequestPayload JAXBElement var1){
PmtAuthAddResponseType response=billPaymentHandler.execute(var1.getValue());
return of.createPmtAuthAddResponse(response); // Used ObjectFactory to create JAXBElement.
}`
`
From all the tutorial I see, they dont need to wrap it as JAXBElement to return the correct type, but the below code does not work for me:
`
`#PayloadRoot( localPart = "PmtAuthAddRequest",
namespace = "http://*com/emb/webseries")
#ResponsePayload
public PmtAuthAddResponseType billPayment(#RequestPayload PmtAuthAddRequestType> var1){
PmtAuthAddResponseType response=billPaymentHandler.execute(var1.getValue());
return response;
}`
`
Do you guys know why? How can I resolve this? Thanks
I tried without wrapping it as JAXBElement, but soap UI return with error message:
`no adapter for endpoint [public com.*.*.*.webseries.billpay.CustPayee50InqResponseType com.*.Endpoint.InquirePayeeEndpoint.inquirepayees(com.*.*.*.webseries.billpay.CustPayee50InqRequestType) throws javax.xml.bind.JAXBException]: Is your endpoint annotated with #Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?</faultstring>
`
Actually solved my own question....
The way to do it is to add #XmlRootElement under generated Java class from JAXB2 with below to correctly mapping:
#XmlRootElement(namespace = "http://..*/emb/webseries",name = "CustPayee50InqRequest")
The name should match with the localPart provided name from #PayloadRoot.
Added both for request and response makes it work for me

ReSTful service getting contradict due to path parameter value has forward slash

I have API like this-
/objectname/name
/objectname/collection/id
Both API's are indirectly related.
Problem occurs when calling first API with name value as "A/B Type". So rest controller actually calling second API rather first (/objectname/A/B Type) because forward slash. How to deal with this situation.
As a side note I am encoding the parameters values.
I developed the restful services using SpringBoot and RestTemplate.
The conflict comes by specifying the name directly in the resource path and passed to the function as a #PathVariable.
Your code looks something like this:
#RequestMapping(value = "objectname/{name}", method = RequestMethod.GET)
public String yourMethodName(#PathVariable String name){
return name;
}
What I would recommend in order to avoid this kind of conflict is (if you're allowed to modify the #RestController or #RepositoryRestResource layers) to pass the value of the object in a #RequestParam
For instance:
#RequestMapping(value = "/objectname", method = RequestMethod.GET)
public String yourMethodName(#RequestParam(name = "name", required = true) String name){
return name;
}
That said, When you are constructing your the request using RestTemplate then you should url encode your name (A%2FB%20Testing) and construct the following url:
http://localhost:8080/objectname?name=A%2FB%20Testing
I tested this locally and worked alright for me.

Basic auth Spring resttemplate examples and they all use an Account.class. What is it?

Here's a typical example of what I find:
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<Account> response = restTemplate.exchange(url,
HttpMethod.GET, request, Account.class);
Account account = response.getBody();
I've googled looking for any reference to the Account.class. So far I've come up empty. I can't find it in Spring 4 JavaDocs. Can anyone tell me where it's documented and is there a Maven dependency that includes the necesssry jars?
Thanks,
Rob
Acoount.class is not a part of any jar. It's a class in your application.
It is used by rest template to map the response to an object. Here is what I mean:
if the response is:
{"name": name, "balance": 1000}
And you have:
class Account {
private String name;
private int balance;
// setters/getters
}
Then the code you provided will return you an instance of the Account.
That's it!

How can I get parameter from the URL in Websphere commerce CmdImpl class

Is there any way to get url parameter in websphere commerce CmdImpl class.
I am trying like this
SmartDataBeanImpl bean = new SmartDataBeanImpl();
HttpServletRequest request = bean.getHttpRequest();
String currencyId = request.getParameter("currencyId");
But I got the NullPointer exception at 3rd Line
Given the assumption that you are working with a controller command then you should access the URL parameters like this:
public void setRequestProperties(TypedProperty pRequestProperties) {
String currencyId = pRequestProperties.getString("currencyId", "");
}
requestProperties.getString("URL", null)
You would see this in most of the OOB controller commands in the validateParameters() method.

Spring MVC GET/redirect/POST

Say I have 2 Spring MVC services:
#RequestMapping(value = "/firstMethod/{param}", method = RequestMethod.GET)
public String firstMethod(#PathVariable String param) {
// ...
// somehow add a POST param
return "redirect:/secondMethod";
}
#RequestMapping(value = "/secondMethod", method = RequestMethod.POST)
public String secondMethod(#RequestParam String param) {
// ...
return "mypage";
}
Could redirect the first method call to second(POST) method?
Using second method as GET or using session is undesirable.
Thanks for your responses!
You should not redirect a HTTP GET to a HTTP POST. HTTP GET and HTTP POST are two different things. They are expected to behave very differently (GET is safe, idempotent and cacheable. POST is idempotent). For more see for example HTTP GET and POST semantics and limitations or http://www.w3schools.com/tags/ref_httpmethods.asp.
What you can do is this: annotate secondMethod also with RequestMethod.GET. Then you should be able to make the desired redirect.
#RequestMapping(value = "/secondMethod", method = {RequestMethod.GET, RequestMethod.POST})
public String secondMethod(#RequestParam String param) {
...
}
But be aware that secondMethod can then be called through HTTP GET requests.