I have set up the following JAXRS service in:
#Path(value="/validationService")
public class ValidationService {
#GET
#Produces(value="text/plain")
#Path(value="{token}")
public String getPropety(#PathParam("token") String token) {
String status = AuthenticationManager.getInstance().getTokenStatus(token);
return status;
}
}
If I hit the url, I receive the proper response on my screen. How can I consume this resouce in the back end? I have set this up using jersey jars and the Cient / CLientBuilder / WebTarget procedure, using TOMCAT, but this does not seem to work with WebsphereApplicationServer 8.5. Any info is appreciated.
answer:
JAX-RS Jersey Client on Websphere 8.5
I was using the incorrect libraries to perform these operations.
I was using: javax.ws.rs to build the client, when I should've been using: org.apache.wink
Related
I need to send HTTP requests from my Quarkus application. Following this guide, I have this RestClient:
#Path("/v1")
#RegisterRestClient
public interface CountriesService {
#GET
#Path("/name/{name}")
Set<Country> getByName(#PathParam String name);
}
In the Path annotation, I can configure the path. But the domain/url to call is defined in a configuration file, according to this paragraph.
# Your configuration properties
org.acme.rest.client.CountriesService/mp-rest/url=https://restcountries.eu/rest #
org.acme.rest.client.CountriesService/mp-rest/scope=javax.inject.Singleton #
In my case, I need this URL to be defined programmatically at runtime, as I receive it as a callback URL.
Is there a way to do that?
Quarkus Rest Client, and Quarkus Rest Client Reactive, implement the MicroProfile Rest specification and as such allow creating client stubs with RestClientBuilder programmatically, e.g.:
public class SomeService {
public Response doWorkAgainstApi(URI apiUri, ApiModel apiModel) {
RemoteApi remoteApi = RestClientBuilder.newBuilder()
.baseUri(apiUri)
.build(RemoteApi.class);
return remoteApi.execute(apiModel);
}
}
See https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_builder_usage
You cannot achieve this with client created with the #RegisterRestClient annotation
I am using Jhipster(Angular + Springboot) Application for my existing project.
I managed to create a controller(app.resource) manually apart from the ones already generated by jhiptser(using .jh file) for achieving a file download functionality.
So, when we start the server we usually initiate two servers i.e gradlew and npm start. The second runs on port 9000 which eventually supports hot reload functionality.(front-end development)
So the problem is, I am able to access those endpoints from the server running on standard 8000 port. However, from the port which is a proxy(9000), the method is returning 404.
I tried to clean build the application several times.
NOTE: The #RequestMapping value on the new controller is different then those present already.
Does this have to do something with spring security?
Thanks in advance.
Here is the previous controller:
#RestController
#RequestMapping("/api")
public class FGAppDiagramResource {
#GetMapping(value = "/fg-app-diagram-downloadFile")
public void getImage(String fileName,String folderName, HttpServletResponse
response){
// Some Code
}
}
Here is my New controller:
#RestController
#RequestMapping("/fileDownload")
public class DownloadFileController {
private final Logger log =
LoggerFactory.getLogger(DownloadFileController.class);
public DownloadFileController() {
super();
}
#Autowired
private ApplicationProperties applicationProperties;
#GetMapping(value = "/fg-app-diagram-downloadFile/{fileName}/{folderName}")
public void getImage(#PathVariable String fileName,#PathVariable String folderName, HttpServletResponse response) {
// Some Code
}
}
Your new controller does not use /api so you must add your endpoint URL /fileDownload to proxy configuration of webpack dev server in webpack/webpack.dev.js
proxy: [{
context: [
/* jhipster-needle-add-entity-to-webpack - JHipster will add entity api paths here */
'/api',
'/fileDownload',
You may want to use /api/fileDownload to avoid changing proxy configuration and also because /api is useful for many other aspects like security and also using HTML5 URL routing strategy in Angular to get rid of # in client routes (see https://github.com/jhipster/generator-jhipster/pull/9098).
/api and /management are namespaces to avoid route conflicts, so it is usually wise to use them for your new endpoints.
I am not sure if this is a feature or bug introduced in version 2.7.1 (or greater) but below is the behavior I noticed when GET request is sent to the BARE CXF web service using web browsers.
Code Snippets:
#WebService
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface HelloWorld
{
#WebMethod(action = "/sayHi")
#WebResult(name = "strResponse", partName = "strResponse")
public String sayHi();
#WebMethod(action = "/sayHiToUser")
#WebResult(name = "strResponse", partName = "strResponse")
String sayHiToUser(#WebParam(name="user") String user);
}
#WebService
public class HelloWorldImpl implements HelloWorld
{
#Override
public String sayHi()
{
return "Hi there...";
}
#Override
public String sayHiToUser(String user)
{
return "Hi " + user;
}
}
<jaxws:endpoint
id="helloWorld"
implementor="com.cxf.service.HelloWorldImpl"
address="http://127.0.0.1:8080/HelloWorld"
endpointName="e:HelloWorld"
xmlns:e="http://service.jaxws.cxf.apache.org/endpoint">
</jaxws:endpoint>
Environment/Configs:
• Java 1.6
• Spring 3.0.0
• CXF 2.7.0 / 2.7.1
• CXF web service starts within embedded Jetty using spring context
Detailed Explanation:
CXF Version 2.7.0:
When I access this web service using web browser with URL "http://127.0.0.1:8080/HelloWorld" (No ?wsdl at the end), I am getting "No such operation" response back in the web browser for both BARE & WRAPPED ParameterStyle which is the expected behavior.
CXF Version 2.7.1 (or greater):
ParameterStyle WRAPPED: When I access this web service using web browser with same URL as above (No ?wsdl at the end), I am getting "No binding operation info while invoking unknown method with params unknown." response back in the web browser which is the expected behavior.
ParameterStyle BARE: When I access this web service using web browser with URL as above (No ?wsdl at the end), I am getting "Hi there..." response back in the web browser. It calls sayHi() operation/method but there is no SOAPAction specified in the GET request header. This is NOT the expected response. I was expecting the same response as ParameterStyle WRAPPED.
Results:
CXF Version: 2.7.0
• URL: Same url as above (Using web browser and no ?wsdl at the end)
• ParameterStyle: BARE / Response: No such operation
• ParameterStyle: WRAPPED / Response: No such operation
• Expected Result? YES
CXF Version: 2.7.1 (or greater)
• URL: Same url as above (Using web browser and no ?wsdl at the end)
• ParameterStyle: BARE / Response: Hi there... / Called sayHi()
• ParameterStyle: WRAPPED / Response: No binding operation info while invoking unknown method with params unknown.
• Expected Result? NO. BARE should return the same result as WRAPPED
From a 1st glance, this looks like the URIMappingInterceptor, see e.g. http://cxf.apache.org/cve-2012-5633.html, which was discarded again in later versions. Perhaps you loaded/added it to your setup by accident (or some defaults changed, not sure).
Btw: That CXF version is quite old (2012), I would highly recommend using a newer one, for both bug & security fixes.
I've defined a RESTful WebService (by using RESTEasy on JBoss AS 7) that consumes a JSON data stream.
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student) {
String output = student.toString();
// Do something...
return Response.status(200).entity(output).build();
}
How can I call my WS from another Spring-based webapp, by properly using the RestTemplate, mapping a Java Object to JSON and passing it as request body?
Note: I'm asking about Spring with the aim to investigate the facilities provided by the framework. I well know that it is possible to do that by defining manually the request body.
Cheers, V.
In the client application, you can create an interface with the same signature as the one you expose on the server side, and the same path.
Then, in the spring configuration file, you can use the RESTeasy client API to generate a proxy connecting to the exposed webservice.
In the client application, it would look like this :
SimpleClient.java
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student);
Config.java
#Bean
public SimpleClient getSimpleClient(){
Client client = ClientFactory.newClient();
WebTarget target = client.target("http://example.com/base/uri");
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
SimpleClient simple = rtarget.proxy(SimpleClient.class);
return simple;
}
Then, in the place where you want to invoke this web service, you inject it with Spring and you can call the method. RESTeasy will search for the webservice matching with with your client (according to the path and the request type) and will create a connection.
Launcher.java
#Resource
private SimpleClient simpleClient;
public void sendMessage(Student student) {
simpleClient.consumeJSON(student);
}
Docs on the RESTesay client API : http://docs.jboss.org/resteasy/docs/3.0.7.Final/userguide/html/RESTEasy_Client_Framework.html
Hope this was helpfull.
I have a JAX-RS Restful webservice. This resource/webservice is asset for us. I am exposing this service url to third party client that would call this service resource. I want to protect this service from another authorised client/vendors.
Thus my question is -
How to protect this.
I thought to include an API key and the API key should be matched with a perticuler client API Key. And I also need to match the client url address means from whcih url the request is coming.
How to do this. How to get the URL that is calling the our rest service. I am deploying my rest in Tomcat 7.0.
Thanks
Update --
#Path("report")
#Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
public class DnaReportResource {
#GET
#Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
public void getDnaReport(#Context UriInfo uri) {
System.out.println(uri.getRequestUri());
System.out.println(uri.getPath());
System.out.println(uri.getAbsolutePath());
System.out.println(uri.getBaseUri());
//MultivaluedMap<String, String> queryParams = uri.getQueryParameters();
}
}
From your servlet handler you can call
String url = request.getRequestURL().toString();
Getting request URL in a servlet
EDIT
did you try
String url = uri.getRequestUri().toString();
What does it return?
You can make use of Tomcat Realm configuration to achieve authenticate your clients.