相关疑难解决方法(0)

从给定的URL获取域名

给定一个URL,我想提取域名(它不应该包含'www'部分).网址可以包含h​​ttp/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)

输入:http://google.com/blah

输出:google.com

java url

111
推荐指数
4
解决办法
17万
查看次数

在Spring MVC中使用Spring的DomainClassConverter的问题

我试图在我的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文档中包含此依赖项:

<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 …
Run Code Online (Sandbox Code Playgroud)

data-binding spring spring-mvc spring-data spring-data-jpa

8
推荐指数
1
解决办法
3957
查看次数