OpenAPI add server at Server Start - spring

I try to modify server list (or default server) on Swagger UI.
1/ On Spring (Not Spring Boot) configuration class I add this :
#Bean
public OpenAPI customOpenAPI() {
log.info("<<<customOpenAPI>>>");
Server server = new Server();
server.setUrl("http://localhost:8091/eatery_spring_rs/rs/");
return new OpenAPI()
.servers(List.of(server));
}
2/ But I never obtain result on Swagger UI, no server is added :
3/ Maven dependencies are :
<!--SWAGGER-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.7</version>
</dependency>
<!-- END -->
5/ Question :
Is it possible to change context path, or add server URL, by program ?

6/ I can affine the description of problem
The context path of application is not complete
It is a Spring 5.3 application, start with web.xml in App Server.
like Tomcat, ...
This is the web.xml definition :
`
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-service.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>`
This mean that the real context path is : [app.context]/rs but in the case of :
#Bean public Docket api(ServletContext servletContext) {
servletContext is equal to [app.context] NOT to [app.context]/rs
It's help ?

Related

#RequestMapping does not redirect me to my url

I am entering Spring MVC but it does not redirect me to the url with the RequestMappin, it only does it with the main page but with the others if I add it manual it does not execute, what can I have wrong?
the url that I use to enter login is http://localhost:8080/spring-mvc/login
#WebServlet(urlPatterns = "/login.do") <------------------This work
public class LoginServlet extends HttpServlet {
private LoginService service = new LoginService();
#Controller
public class LoginController { <!---------------------------This not-->
#RequestMapping(value="/login")
#ResponseBody
public String decirHola() {
return "Hola a todos";
}
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
<servlet>
`<servlet-name>dispatcher</servlet-name>`
` <servlet-class>`
` org.springframework.web.servlet.DispatcherServlet`
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/todo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
``` <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
<!-- pom -->
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>`

No injection source found for Multipart feature

when I try to deploy my maven project to Glassfish 5, I get the following error:
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.test.Resources.AccountResource.addProfilePicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.test.Resources.AccountResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor#1efcbc1f]}, definitionMethod=public javax.ws.rs.core.Response com.test.Resources.AccountResource.addProfilePicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=profilePicture, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=profilePicture, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']. Please see server.log for more details.
This is the code which causes the troubles:
#POST
#Path("addProfilePicture")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addProfilePicture(#FormDataParam("profilePicture") InputStream pic,
#FormDataParam("profilePicture") FormDataContentDisposition formDataContentDisposition){
return Response.ok().build();
}
my web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
According to similar answers, I should add the maven dependency and register the MultiPartFeature.
As you can see, I did both.
#ApplicationPath("/")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<>();
classes.add(MultiPartFeature.class);
System.out.println("added multipart feature");
classes.add(AccountResource.class);
return classes;
}
}
and here are my maven dependencies:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
I already tried to change the scope to provided and used ResourceConfig instead of Application.
So here's the thing. You have two different application configurations: the web.xml and the ApplicationConfig class. These are to completely separate configurations, which are both valid all by themselves. The ApplicationConfig class is the one that registers the MultiPartFeature, but it seems like the configuration that is being used is the web.xml. I never really tested what happens when you have two different application configurations, but it seems like your web.xml is taking precedence. And in your web.xml, you are not configuring the MultiPartFeature.
If you want to just use the web.xml, then you can check out this answer for how out can configure it.
You can however just delete the entire web.xml. This will cause the ApplicationConfig to kick it. But notice the two differences: the #ApplicationPath on the ApplicationConfig acts like the url-mapping in the web.xml. So if you delete the web.xml, then the base path will just be / (or nothing) instead of the /webapi like you have in the web.xml
If you decide the delete the web.xml, I suggest you use ResourceConfig instead of Application. You can get some good information about this class and different configuration possibilities in this post from What exactly is the ResourceConfig class in Jersey 2?

Access to the specified resource has been forbidden in simple Spring webservice Demo

I have created simple spring security demo with REST webservice.
I have spent lot of hours on It.Need strong pointers regarding simple working spring security for rest webservice with latest versions.
My Controller is
#Controller
public class RestContoller {
#RequestMapping(value = "/countryJSONProduce", method = RequestMethod.GET)
public ResponseEntity<CountryDetail> getCountryJSON() {
CountryDetail countryDetail = new CountryDetail("Values");
ResponseEntity<CountryDetail> rentity = new ResponseEntity<CountryDetail>(countryDetail, HttpStatus.OK);
return rentity;
}
#RequestMapping(value = "/countryJSONConsume", consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public String consumeJSON(#RequestBody CountryDetail countryDetail) {
System.out.println("Country Detail Example");
return "home";
}
}
web.xml is
<servlet>
<servlet-name>springrest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springrest</servlet-name>
<url-pattern>/hello/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml
/WEB-INF/springrest-servlet.xml
</param-value>
</context-param>
my pom.xml is
<properties>
<spring.version>4.1.0.RELEASE</spring.version>
<springsecurity.version>4.0.2.RELEASE</springsecurity.version>
</properties>
<dependencies>
<!-- for Jsp use -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Spring mvc and Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring mvc and Core -->
<!-- JSON Response Spring Framework 4.1, the minimum jackson version should
be 2.1 -->
<!-- Compatiable Spring Framework 4.1 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<!-- Spring Authentication Start -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<!-- Spring Authentication End -->
</dependencies>
When I Run the code sometimes it shows popup in my Eclipse Mars INTENAL
browser for user and password.when i put my credentials it will goes to
localhost:8080/SpringMavenRest2/ welcome page ok.when I hit the
url localhost:8080/SpringMavenRest2/hello/countryJSONProduce
which is calling my first service.. it is showing the Error :Access to
the specified resource has been forbidden.403
Even I put user name and
password as basic auth.Now I am testing this second url FROM CHROME
POSTMAN CLIENT.
I am using this configuration Java 1.8 ,Tomcat 8.0
spring.version4.1.0.RELEASE ,springsecurity.version 4.0.2.RELEASE.
and maven 3.3
Its
working well without authentication.Could you give any best referenced demo
for spring security with basic authentication. I have refered this also
http://www.mkyong.com/spring-security/spring-security-hello-world-example/
Try this :
<security:intercept-url pattern="/hello/**" access="hasRole('ROLE_USER'') "/>
and / or
<security:intercept-url pattern="/**" access="hasAnyRole('IS_AUTHENTICATED_ANONYMOUSLY','ROLE_USER')"/>

ResourceConfig instance does not contain any root resource classes

I can't seem to get my jersey resources recognized in my app. According to everything I read online, there are two possible cases where this may happen.
There is no valid package declared in the web.xml under com.sun.jersey.config.property.packages.
This is my web.xml:
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>my.package</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
:
2. There is no valid resource class in my.package.
My resource class:
package my.package;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
#Path("/")
class MyClass{
#GET
public Response asdf(){
return Response.ok().build();
}
#Path("/test")
#GET
public Response test(){
return Response.ok().build();
}
}
As far as I can tell, neither of these two conditions are satisfied.
For the sake of completion, here are my dependencies:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
</dependency>
Any ideas as to why I might be getting this error?
#Path("/")
class MyClass{
The class isn't visible to Jersey. Try changing it to (note the addition of "public"):
#Path("/")
public class MyClass{

Spring MVC Files org.springframework.web.servlet.DispatcherServlet noHandlerFound

I have tried multiple times, but still I am getting these issue, but unable to understand from where this is arising.
Oct 2, 2013 1:50:37 PM
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI
[/Demo/greeting.html] in DispatcherServlet with name 'demoServlet'
My web.xml
<servlet>
<servlet-name>demoServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demoServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
And My servlet-config.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.demo.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/content/" p:suffix=".jsp"></bean>
My HelloController
#Controller
public class HelloController {
#RequestMapping(value ="/greeting")
public String sayHello(Model model){
model.addAttribute("greeting","Hello World !");
return "hello";
}
}
I have no idea but this solved my problem!! [Would Like to know the reason for the original problem]
I changed
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
to
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
and it ran perfectly fine :)
Try to replace:
#RequestMapping(value ="/greeting")
with:
#RequestMapping(value ="/greeting.html")

Resources