给定一个URL,我想提取域名(它不应该包含'www'部分).网址可以包含http/https.这是我写的java代码.虽然它似乎工作正常,有没有更好的方法或有一些边缘情况,可能会失败.
public static String getDomainName(String url) throws MalformedURLException{
if(!url.startsWith("http") && !url.startsWith("https")){
url = "http://" + url;
}
URL netUrl = new URL(url);
String host = netUrl.getHost();
if(host.startsWith("www")){
host = host.substring("www".length()+1);
}
return host;
}
Run Code Online (Sandbox Code Playgroud)
输出:google.com
我试图在我的Spring MVC项目中使用Spring的DomainClassConverter功能.(我对Spring MVC和Spring只有非常基本的知识,对于任何天真的问题都提前道歉).
来自API文档:
The DomainClassConverter allows you to use domain types in your Spring MVC controller
method signatures directly, so that you don't have to manually lookup the instances via
the repository: (PS: Example 1.20)
Run Code Online (Sandbox Code Playgroud)
我从上面的理解是,我不必编写finder方法,Spring提供了User对象.所以这些是我做的步骤:
包括以下XML行applicationcontext.xml.
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.rl.userservice.controller.UserConverter"/>
</list>
</property>
Run Code Online (Sandbox Code Playgroud)
pom.xml根据Spring Data REST文档中包含此依赖项:
Run Code Online (Sandbox Code Playgroud)<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-webmvc</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> </dependency> </dependencies>
我的控制器如下所示:
Run Code Online (Sandbox Code Playgroud)@Controller @RequestMapping(value = "/api/newuser") public class NewUserServiceController { @Autowired …